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 $
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>
71 #include <vm/vm_page.h>
73 static int change_dir __P((struct nameidata *ndp, struct proc *p));
74 static void checkdirs __P((struct vnode *olddp));
75 static int chroot_refuse_vdir_fds __P((struct filedesc *fdp));
76 static int getutimes __P((const struct timeval *, struct timespec *));
77 static int setfown __P((struct proc *, struct vnode *, uid_t, gid_t));
78 static int setfmode __P((struct proc *, struct vnode *, int));
79 static int setfflags __P((struct proc *, struct vnode *, int));
80 static int setutimes __P((struct proc *, struct vnode *,
81 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_
107 register struct mount_args /* {
108 syscallarg(char *) type;
109 syscallarg(char *) path;
110 syscallarg(int) flags;
111 syscallarg(caddr_t) data;
116 struct vfsconf *vfsp;
117 int error, flag = 0, flag2 = 0;
123 char fstypename[MFSNAMELEN];
125 if (usermount == 0 && (error = suser(p)))
128 * Do not allow NFS export by non-root users.
130 if (SCARG(uap, flags) & MNT_EXPORTED) {
136 * Silently enforce MNT_NOSUID and MNT_NODEV for non-root users
138 if (suser_xxx(p->p_ucred, 0, 0))
139 SCARG(uap, flags) |= MNT_NOSUID | MNT_NODEV;
141 * Get vnode to be covered
143 NDINIT(&nd, LOOKUP, FOLLOW | LOCKLEAF, UIO_USERSPACE,
144 SCARG(uap, path), p);
145 if ((error = namei(&nd)) != 0)
147 NDFREE(&nd, NDF_ONLY_PNBUF);
149 if (SCARG(uap, flags) & MNT_UPDATE) {
150 if ((vp->v_flag & VROOT) == 0) {
156 flag2 = mp->mnt_kern_flag;
158 * We only allow the filesystem to be reloaded if it
159 * is currently mounted read-only.
161 if ((SCARG(uap, flags) & MNT_RELOAD) &&
162 ((mp->mnt_flag & MNT_RDONLY) == 0)) {
164 return (EOPNOTSUPP); /* Needs translation */
167 * Only root, or the user that did the original mount is
168 * permitted to update it.
170 if (mp->mnt_stat.f_owner != p->p_ucred->cr_uid &&
171 (error = suser(p))) {
175 if (vfs_busy(mp, LK_NOWAIT, 0, p)) {
179 simple_lock(&vp->v_interlock);
180 if ((vp->v_flag & VMOUNT) != 0 ||
181 vp->v_mountedhere != NULL) {
182 simple_unlock(&vp->v_interlock);
187 vp->v_flag |= VMOUNT;
188 simple_unlock(&vp->v_interlock);
190 SCARG(uap, flags) & (MNT_RELOAD | MNT_FORCE | MNT_UPDATE);
191 VOP_UNLOCK(vp, 0, p);
195 * If the user is not root, ensure that they own the directory
196 * onto which we are attempting to mount.
198 if ((error = VOP_GETATTR(vp, &va, p->p_ucred, p)) ||
199 (va.va_uid != p->p_ucred->cr_uid &&
200 (error = suser(p)))) {
204 if ((error = vinvalbuf(vp, V_SAVE, p->p_ucred, p, 0, 0)) != 0) {
208 if (vp->v_type != VDIR) {
214 * Historically filesystem types were identified by number. If we
215 * get an integer for the filesystem type instead of a string, we
216 * check to see if it matches one of the historic filesystem types.
218 fstypenum = (uintptr_t)SCARG(uap, type);
219 if (fstypenum < maxvfsconf) {
220 for (vfsp = vfsconf; vfsp; vfsp = vfsp->vfc_next)
221 if (vfsp->vfc_typenum == fstypenum)
227 strncpy(fstypename, vfsp->vfc_name, MFSNAMELEN);
229 #endif /* COMPAT_43 */
230 if ((error = copyinstr(SCARG(uap, type), fstypename, MFSNAMELEN, NULL)) != 0) {
234 for (vfsp = vfsconf; vfsp; vfsp = vfsp->vfc_next)
235 if (!strcmp(vfsp->vfc_name, fstypename))
240 /* Only load modules for root (very important!) */
241 if ((error = suser(p)) != 0) {
245 error = linker_load_file(fstypename, &lf);
246 if (error || lf == NULL) {
253 /* lookup again, see if the VFS was loaded */
254 for (vfsp = vfsconf; vfsp; vfsp = vfsp->vfc_next)
255 if (!strcmp(vfsp->vfc_name, fstypename))
259 linker_file_unload(lf);
264 simple_lock(&vp->v_interlock);
265 if ((vp->v_flag & VMOUNT) != 0 ||
266 vp->v_mountedhere != NULL) {
267 simple_unlock(&vp->v_interlock);
271 vp->v_flag |= VMOUNT;
272 simple_unlock(&vp->v_interlock);
275 * Allocate and initialize the filesystem.
277 mp = malloc(sizeof(struct mount), M_MOUNT, M_WAITOK);
278 bzero((char *)mp, (u_long)sizeof(struct mount));
279 TAILQ_INIT(&mp->mnt_nvnodelist);
280 TAILQ_INIT(&mp->mnt_reservedvnlist);
281 mp->mnt_nvnodelistsize = 0;
282 lockinit(&mp->mnt_lock, PVFS, "vfslock", 0, LK_NOPAUSE);
283 (void)vfs_busy(mp, LK_NOWAIT, 0, p);
284 mp->mnt_op = vfsp->vfc_vfsops;
286 vfsp->vfc_refcount++;
287 mp->mnt_stat.f_type = vfsp->vfc_typenum;
288 mp->mnt_flag |= vfsp->vfc_flags & MNT_VISFLAGMASK;
289 strncpy(mp->mnt_stat.f_fstypename, vfsp->vfc_name, MFSNAMELEN);
290 mp->mnt_vnodecovered = vp;
291 mp->mnt_stat.f_owner = p->p_ucred->cr_uid;
292 mp->mnt_iosize_max = DFLTPHYS;
293 VOP_UNLOCK(vp, 0, p);
296 * Set the mount level flags.
298 if (SCARG(uap, flags) & MNT_RDONLY)
299 mp->mnt_flag |= MNT_RDONLY;
300 else if (mp->mnt_flag & MNT_RDONLY)
301 mp->mnt_kern_flag |= MNTK_WANTRDWR;
302 mp->mnt_flag &=~ (MNT_NOSUID | MNT_NOEXEC | MNT_NODEV |
303 MNT_SYNCHRONOUS | MNT_UNION | MNT_ASYNC | MNT_NOATIME |
304 MNT_NOSYMFOLLOW | MNT_IGNORE |
305 MNT_NOCLUSTERR | MNT_NOCLUSTERW | MNT_SUIDDIR);
306 mp->mnt_flag |= SCARG(uap, flags) & (MNT_NOSUID | MNT_NOEXEC |
307 MNT_NODEV | MNT_SYNCHRONOUS | MNT_UNION | MNT_ASYNC | MNT_FORCE |
308 MNT_NOSYMFOLLOW | MNT_IGNORE |
309 MNT_NOATIME | MNT_NOCLUSTERR | MNT_NOCLUSTERW | MNT_SUIDDIR);
311 * Mount the filesystem.
312 * XXX The final recipients of VFS_MOUNT just overwrite the ndp they
313 * get. No freeing of cn_pnbuf.
315 error = VFS_MOUNT(mp, SCARG(uap, path), SCARG(uap, data), &nd, p);
316 if (mp->mnt_flag & MNT_UPDATE) {
317 if (mp->mnt_kern_flag & MNTK_WANTRDWR)
318 mp->mnt_flag &= ~MNT_RDONLY;
319 mp->mnt_flag &=~ (MNT_UPDATE | MNT_RELOAD | MNT_FORCE);
320 mp->mnt_kern_flag &=~ MNTK_WANTRDWR;
323 mp->mnt_kern_flag = flag2;
325 if ((mp->mnt_flag & MNT_RDONLY) == 0) {
326 if (mp->mnt_syncer == NULL)
327 error = vfs_allocate_syncvnode(mp);
329 if (mp->mnt_syncer != NULL)
330 vrele(mp->mnt_syncer);
331 mp->mnt_syncer = NULL;
334 simple_lock(&vp->v_interlock);
335 vp->v_flag &= ~VMOUNT;
336 simple_unlock(&vp->v_interlock);
340 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY, p);
342 * Put the new filesystem on the mount list after root.
346 simple_lock(&vp->v_interlock);
347 vp->v_flag &= ~VMOUNT;
348 vp->v_mountedhere = mp;
349 simple_unlock(&vp->v_interlock);
350 simple_lock(&mountlist_slock);
351 TAILQ_INSERT_TAIL(&mountlist, mp, mnt_list);
352 simple_unlock(&mountlist_slock);
354 VOP_UNLOCK(vp, 0, p);
355 if ((mp->mnt_flag & MNT_RDONLY) == 0)
356 error = vfs_allocate_syncvnode(mp);
358 if ((error = VFS_START(mp, 0, p)) != 0)
361 simple_lock(&vp->v_interlock);
362 vp->v_flag &= ~VMOUNT;
363 simple_unlock(&vp->v_interlock);
364 mp->mnt_vfc->vfc_refcount--;
366 free((caddr_t)mp, M_MOUNT);
373 * Scan all active processes to see if any of them have a current
374 * or root directory onto which the new filesystem has just been
375 * mounted. If so, replace them with the new mount point.
381 struct filedesc *fdp;
385 if (olddp->v_usecount == 1)
387 if (VFS_ROOT(olddp->v_mountedhere, &newdp))
388 panic("mount: lost mount");
389 LIST_FOREACH(p, &allproc, p_list) {
391 if (fdp->fd_cdir == olddp) {
394 fdp->fd_cdir = newdp;
396 if (fdp->fd_rdir == olddp) {
399 fdp->fd_rdir = newdp;
402 if (rootvnode == olddp) {
411 * Unmount a file system.
413 * Note: unmount takes a path to the vnode mounted on as argument,
414 * not special file (as before).
416 #ifndef _SYS_SYSPROTO_H_
417 struct unmount_args {
426 register struct unmount_args /* {
427 syscallarg(char *) path;
428 syscallarg(int) flags;
431 register struct vnode *vp;
436 NDINIT(&nd, LOOKUP, FOLLOW | LOCKLEAF, UIO_USERSPACE,
437 SCARG(uap, path), p);
438 if ((error = namei(&nd)) != 0)
441 NDFREE(&nd, NDF_ONLY_PNBUF);
445 * Only root, or the user that did the original mount is
446 * permitted to unmount this filesystem.
448 if ((mp->mnt_stat.f_owner != p->p_ucred->cr_uid) &&
449 (error = suser(p))) {
455 * Don't allow unmounting the root file system.
457 if (mp->mnt_flag & MNT_ROOTFS) {
463 * Must be the root of the filesystem
465 if ((vp->v_flag & VROOT) == 0) {
470 return (dounmount(mp, SCARG(uap, flags), p));
474 * Do the actual file system unmount.
477 dounmount(mp, flags, p)
478 register struct mount *mp;
482 struct vnode *coveredvp;
486 simple_lock(&mountlist_slock);
487 if (mp->mnt_kern_flag & MNTK_UNMOUNT) {
488 simple_unlock(&mountlist_slock);
491 mp->mnt_kern_flag |= MNTK_UNMOUNT;
492 /* Allow filesystems to detect that a forced unmount is in progress. */
493 if (flags & MNT_FORCE)
494 mp->mnt_kern_flag |= MNTK_UNMOUNTF;
495 error = lockmgr(&mp->mnt_lock, LK_DRAIN | LK_INTERLOCK |
496 ((flags & MNT_FORCE) ? 0 : LK_NOWAIT), &mountlist_slock, p);
498 mp->mnt_kern_flag &= ~(MNTK_UNMOUNT | MNTK_UNMOUNTF);
499 if (mp->mnt_kern_flag & MNTK_MWAIT)
504 if (mp->mnt_flag & MNT_EXPUBLIC)
505 vfs_setpublicfs(NULL, NULL, NULL);
507 vfs_msync(mp, MNT_WAIT);
508 async_flag = mp->mnt_flag & MNT_ASYNC;
509 mp->mnt_flag &=~ MNT_ASYNC;
510 cache_purgevfs(mp); /* remove cache entries for this file sys */
511 if (mp->mnt_syncer != NULL)
512 vrele(mp->mnt_syncer);
513 if (((mp->mnt_flag & MNT_RDONLY) ||
514 (error = VFS_SYNC(mp, MNT_WAIT, p->p_ucred, p)) == 0) ||
516 error = VFS_UNMOUNT(mp, flags, p);
517 simple_lock(&mountlist_slock);
519 if ((mp->mnt_flag & MNT_RDONLY) == 0 && mp->mnt_syncer == NULL)
520 (void) vfs_allocate_syncvnode(mp);
521 mp->mnt_kern_flag &= ~(MNTK_UNMOUNT | MNTK_UNMOUNTF);
522 mp->mnt_flag |= async_flag;
523 lockmgr(&mp->mnt_lock, LK_RELEASE | LK_INTERLOCK | LK_REENABLE,
524 &mountlist_slock, p);
525 if (mp->mnt_kern_flag & MNTK_MWAIT)
529 TAILQ_REMOVE(&mountlist, mp, mnt_list);
530 if ((coveredvp = mp->mnt_vnodecovered) != NULLVP) {
531 coveredvp->v_mountedhere = (struct mount *)0;
534 mp->mnt_vfc->vfc_refcount--;
535 if (!TAILQ_EMPTY(&mp->mnt_nvnodelist))
536 panic("unmount: dangling vnode");
537 lockmgr(&mp->mnt_lock, LK_RELEASE | LK_INTERLOCK, &mountlist_slock, p);
538 if (mp->mnt_kern_flag & MNTK_MWAIT)
540 free((caddr_t)mp, M_MOUNT);
545 * Sync each mounted filesystem.
547 #ifndef _SYS_SYSPROTO_H_
554 static int syncprt = 0;
555 SYSCTL_INT(_debug, OID_AUTO, syncprt, CTLFLAG_RW, &syncprt, 0, "");
562 struct sync_args *uap;
564 register struct mount *mp, *nmp;
567 simple_lock(&mountlist_slock);
568 for (mp = TAILQ_FIRST(&mountlist); mp != NULL; mp = nmp) {
569 if (vfs_busy(mp, LK_NOWAIT, &mountlist_slock, p)) {
570 nmp = TAILQ_NEXT(mp, mnt_list);
573 if ((mp->mnt_flag & MNT_RDONLY) == 0) {
574 asyncflag = mp->mnt_flag & MNT_ASYNC;
575 mp->mnt_flag &= ~MNT_ASYNC;
576 vfs_msync(mp, MNT_NOWAIT);
577 VFS_SYNC(mp, MNT_NOWAIT,
578 ((p != NULL) ? p->p_ucred : NOCRED), p);
579 mp->mnt_flag |= asyncflag;
581 simple_lock(&mountlist_slock);
582 nmp = TAILQ_NEXT(mp, mnt_list);
585 simple_unlock(&mountlist_slock);
588 * XXX don't call vfs_bufstats() yet because that routine
589 * was not imported in the Lite2 merge.
594 #endif /* DIAGNOSTIC */
599 /* XXX PRISON: could be per prison flag */
600 static int prison_quotas;
602 SYSCTL_INT(_kern_prison, OID_AUTO, quotas, CTLFLAG_RW, &prison_quotas, 0, "");
606 * Change filesystem quotas.
608 #ifndef _SYS_SYSPROTO_H_
609 struct quotactl_args {
620 register struct quotactl_args /* {
621 syscallarg(char *) path;
624 syscallarg(caddr_t) arg;
627 register struct mount *mp;
631 if (p->p_prison && !prison_quotas)
633 NDINIT(&nd, LOOKUP, FOLLOW, UIO_USERSPACE, SCARG(uap, path), p);
634 if ((error = namei(&nd)) != 0)
636 mp = nd.ni_vp->v_mount;
637 NDFREE(&nd, NDF_ONLY_PNBUF);
639 return (VFS_QUOTACTL(mp, SCARG(uap, cmd), SCARG(uap, uid),
640 SCARG(uap, arg), p));
644 * Get filesystem statistics.
646 #ifndef _SYS_SYSPROTO_H_
656 register struct statfs_args /* {
657 syscallarg(char *) path;
658 syscallarg(struct statfs *) buf;
661 register struct mount *mp;
662 register struct statfs *sp;
667 NDINIT(&nd, LOOKUP, FOLLOW, UIO_USERSPACE, SCARG(uap, path), p);
668 if ((error = namei(&nd)) != 0)
670 mp = nd.ni_vp->v_mount;
672 NDFREE(&nd, NDF_ONLY_PNBUF);
674 error = VFS_STATFS(mp, sp, p);
677 sp->f_flags = mp->mnt_flag & MNT_VISFLAGMASK;
678 if (suser_xxx(p->p_ucred, 0, 0)) {
679 bcopy((caddr_t)sp, (caddr_t)&sb, sizeof(sb));
680 sb.f_fsid.val[0] = sb.f_fsid.val[1] = 0;
683 return (copyout((caddr_t)sp, (caddr_t)SCARG(uap, buf), sizeof(*sp)));
687 * Get filesystem statistics.
689 #ifndef _SYS_SYSPROTO_H_
690 struct fstatfs_args {
699 register struct fstatfs_args /* {
701 syscallarg(struct statfs *) buf;
706 register struct statfs *sp;
710 if ((error = getvnode(p->p_fd, SCARG(uap, fd), &fp)) != 0)
712 mp = ((struct vnode *)fp->f_data)->v_mount;
716 error = VFS_STATFS(mp, sp, p);
719 sp->f_flags = mp->mnt_flag & MNT_VISFLAGMASK;
720 if (suser_xxx(p->p_ucred, 0, 0)) {
721 bcopy((caddr_t)sp, (caddr_t)&sb, sizeof(sb));
722 sb.f_fsid.val[0] = sb.f_fsid.val[1] = 0;
725 return (copyout((caddr_t)sp, (caddr_t)SCARG(uap, buf), sizeof(*sp)));
729 * Get statistics on all filesystems.
731 #ifndef _SYS_SYSPROTO_H_
732 struct getfsstat_args {
741 register struct getfsstat_args /* {
742 syscallarg(struct statfs *) buf;
743 syscallarg(long) bufsize;
744 syscallarg(int) flags;
747 register struct mount *mp, *nmp;
748 register struct statfs *sp;
750 long count, maxcount, error;
752 maxcount = SCARG(uap, bufsize) / sizeof(struct statfs);
753 sfsp = (caddr_t)SCARG(uap, buf);
755 simple_lock(&mountlist_slock);
756 for (mp = TAILQ_FIRST(&mountlist); mp != NULL; mp = nmp) {
757 if (vfs_busy(mp, LK_NOWAIT, &mountlist_slock, p)) {
758 nmp = TAILQ_NEXT(mp, mnt_list);
761 if (sfsp && count < maxcount) {
764 * If MNT_NOWAIT or MNT_LAZY is specified, do not
765 * refresh the fsstat cache. MNT_NOWAIT or MNT_LAZY
766 * overrides MNT_WAIT.
768 if (((SCARG(uap, flags) & (MNT_LAZY|MNT_NOWAIT)) == 0 ||
769 (SCARG(uap, flags) & MNT_WAIT)) &&
770 (error = VFS_STATFS(mp, sp, p))) {
771 simple_lock(&mountlist_slock);
772 nmp = TAILQ_NEXT(mp, mnt_list);
776 sp->f_flags = mp->mnt_flag & MNT_VISFLAGMASK;
777 error = copyout((caddr_t)sp, sfsp, sizeof(*sp));
785 simple_lock(&mountlist_slock);
786 nmp = TAILQ_NEXT(mp, mnt_list);
789 simple_unlock(&mountlist_slock);
790 if (sfsp && count > maxcount)
791 p->p_retval[0] = maxcount;
793 p->p_retval[0] = count;
798 * Change current working directory to a given file descriptor.
800 #ifndef _SYS_SYSPROTO_H_
809 struct fchdir_args /* {
813 register struct filedesc *fdp = p->p_fd;
814 struct vnode *vp, *tdp;
819 if ((error = getvnode(fdp, SCARG(uap, fd), &fp)) != 0)
821 vp = (struct vnode *)fp->f_data;
823 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY, p);
824 if (vp->v_type != VDIR)
827 error = VOP_ACCESS(vp, VEXEC, p->p_ucred, p);
828 while (!error && (mp = vp->v_mountedhere) != NULL) {
829 if (vfs_busy(mp, 0, 0, p))
831 error = VFS_ROOT(mp, &tdp);
842 VOP_UNLOCK(vp, 0, p);
849 * Change current working directory (``.'').
851 #ifndef _SYS_SYSPROTO_H_
860 struct chdir_args /* {
861 syscallarg(char *) path;
864 register struct filedesc *fdp = p->p_fd;
868 NDINIT(&nd, LOOKUP, FOLLOW | LOCKLEAF, UIO_USERSPACE,
869 SCARG(uap, path), p);
870 if ((error = change_dir(&nd, p)) != 0)
872 NDFREE(&nd, NDF_ONLY_PNBUF);
874 fdp->fd_cdir = nd.ni_vp;
879 * Helper function for raised chroot(2) security function: Refuse if
880 * any filedescriptors are open directories.
883 chroot_refuse_vdir_fds(fdp)
884 struct filedesc *fdp;
891 for (fd = 0; fd < fdp->fd_nfiles ; fd++) {
892 error = getvnode(fdp, fd, &fp);
895 vp = (struct vnode *)fp->f_data;
896 if (vp->v_type != VDIR)
904 * This sysctl determines if we will allow a process to chroot(2) if it
905 * has a directory open:
906 * 0: disallowed for all processes.
907 * 1: allowed for processes that were not already chroot(2)'ed.
908 * 2: allowed for all processes.
911 static int chroot_allow_open_directories = 1;
913 SYSCTL_INT(_kern, OID_AUTO, chroot_allow_open_directories, CTLFLAG_RW,
914 &chroot_allow_open_directories, 0, "");
917 * Change notion of root (``/'') directory.
919 #ifndef _SYS_SYSPROTO_H_
928 struct chroot_args /* {
929 syscallarg(char *) path;
932 register struct filedesc *fdp = p->p_fd;
936 error = suser_xxx(0, p, PRISON_ROOT);
939 if (chroot_allow_open_directories == 0 ||
940 (chroot_allow_open_directories == 1 && fdp->fd_rdir != rootvnode))
941 error = chroot_refuse_vdir_fds(fdp);
944 NDINIT(&nd, LOOKUP, FOLLOW | LOCKLEAF, UIO_USERSPACE,
945 SCARG(uap, path), p);
946 if ((error = change_dir(&nd, p)) != 0)
948 NDFREE(&nd, NDF_ONLY_PNBUF);
950 fdp->fd_rdir = nd.ni_vp;
952 fdp->fd_jdir = nd.ni_vp;
959 * Common routine for chroot and chdir.
963 register struct nameidata *ndp;
973 if (vp->v_type != VDIR)
976 error = VOP_ACCESS(vp, VEXEC, p->p_ucred, p);
980 VOP_UNLOCK(vp, 0, p);
985 * Check permissions, allocate an open file structure,
986 * and call the device open routine if any.
988 #ifndef _SYS_SYSPROTO_H_
998 register struct open_args /* {
999 syscallarg(char *) path;
1000 syscallarg(int) flags;
1001 syscallarg(int) mode;
1004 register struct filedesc *fdp = p->p_fd;
1005 register struct file *fp;
1006 register struct vnode *vp;
1007 int cmode, flags, oflags;
1009 int type, indx, error;
1011 struct nameidata nd;
1013 oflags = SCARG(uap, flags);
1014 if ((oflags & O_ACCMODE) == O_ACCMODE)
1016 flags = FFLAGS(oflags);
1017 error = falloc(p, &nfp, &indx);
1021 cmode = ((SCARG(uap, mode) &~ fdp->fd_cmask) & ALLPERMS) &~ S_ISTXT;
1022 NDINIT(&nd, LOOKUP, FOLLOW, UIO_USERSPACE, SCARG(uap, path), p);
1023 p->p_dupfd = -indx - 1; /* XXX check for fdopen */
1025 * Bump the ref count to prevent another process from closing
1026 * the descriptor while we are blocked in vn_open()
1029 error = vn_open(&nd, flags, cmode);
1032 * release our own reference
1037 * handle special fdopen() case. bleh. dupfdopen() is
1038 * responsible for dropping the old contents of ofiles[indx]
1041 if ((error == ENODEV || error == ENXIO) &&
1042 p->p_dupfd >= 0 && /* XXX from fdopen */
1044 dupfdopen(p, fdp, indx, p->p_dupfd, flags, error)) == 0) {
1045 p->p_retval[0] = indx;
1049 * Clean up the descriptor, but only if another thread hadn't
1050 * replaced or closed it.
1052 if (fdp->fd_ofiles[indx] == fp) {
1053 fdp->fd_ofiles[indx] = NULL;
1057 if (error == ERESTART)
1062 NDFREE(&nd, NDF_ONLY_PNBUF);
1066 * There should be 2 references on the file, one from the descriptor
1067 * table, and one for us.
1069 * Handle the case where someone closed the file (via its file
1070 * descriptor) while we were blocked. The end result should look
1071 * like opening the file succeeded but it was immediately closed.
1073 if (fp->f_count == 1) {
1074 KASSERT(fdp->fd_ofiles[indx] != fp,
1075 ("Open file descriptor lost all refs"));
1076 VOP_UNLOCK(vp, 0, p);
1077 vn_close(vp, flags & FMASK, fp->f_cred, p);
1079 p->p_retval[0] = indx;
1083 fp->f_data = (caddr_t)vp;
1084 fp->f_flag = flags & FMASK;
1086 fp->f_type = (vp->v_type == VFIFO ? DTYPE_FIFO : DTYPE_VNODE);
1087 if (flags & (O_EXLOCK | O_SHLOCK)) {
1088 lf.l_whence = SEEK_SET;
1091 if (flags & O_EXLOCK)
1092 lf.l_type = F_WRLCK;
1094 lf.l_type = F_RDLCK;
1096 if ((flags & FNONBLOCK) == 0)
1098 VOP_UNLOCK(vp, 0, p);
1099 if ((error = VOP_ADVLOCK(vp, (caddr_t)fp, F_SETLK, &lf, type)) != 0) {
1101 * lock request failed. Normally close the descriptor
1102 * but handle the case where someone might have dup()d
1103 * it when we weren't looking. One reference is
1104 * owned by the descriptor array, the other by us.
1106 if (fdp->fd_ofiles[indx] == fp) {
1107 fdp->fd_ofiles[indx] = NULL;
1113 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY, p);
1114 fp->f_flag |= FHASLOCK;
1116 /* assert that vn_open created a backing object if one is needed */
1117 KASSERT(!vn_canvmio(vp) || VOP_GETVOBJECT(vp, NULL) == 0,
1118 ("open: vmio vnode has no backing object after vn_open"));
1119 VOP_UNLOCK(vp, 0, p);
1122 * release our private reference, leaving the one associated with the
1123 * descriptor table intact.
1126 p->p_retval[0] = indx;
1134 #ifndef _SYS_SYSPROTO_H_
1135 struct ocreat_args {
1143 register struct ocreat_args /* {
1144 syscallarg(char *) path;
1145 syscallarg(int) mode;
1148 struct open_args /* {
1149 syscallarg(char *) path;
1150 syscallarg(int) flags;
1151 syscallarg(int) mode;
1154 SCARG(&nuap, path) = SCARG(uap, path);
1155 SCARG(&nuap, mode) = SCARG(uap, mode);
1156 SCARG(&nuap, flags) = O_WRONLY | O_CREAT | O_TRUNC;
1157 return (open(p, &nuap));
1159 #endif /* COMPAT_43 */
1162 * Create a special file.
1164 #ifndef _SYS_SYSPROTO_H_
1175 register struct mknod_args /* {
1176 syscallarg(char *) path;
1177 syscallarg(int) mode;
1178 syscallarg(int) dev;
1181 register struct vnode *vp;
1185 struct nameidata nd;
1187 switch (SCARG(uap, mode) & S_IFMT) {
1193 error = suser_xxx(0, p, PRISON_ROOT);
1199 NDINIT(&nd, CREATE, LOCKPARENT, UIO_USERSPACE, SCARG(uap, path), p);
1200 if ((error = namei(&nd)) != 0)
1207 vattr.va_mode = (SCARG(uap, mode) & ALLPERMS) &~ p->p_fd->fd_cmask;
1208 vattr.va_rdev = SCARG(uap, dev);
1211 switch (SCARG(uap, mode) & S_IFMT) {
1212 case S_IFMT: /* used by badsect to flag bad sectors */
1213 vattr.va_type = VBAD;
1216 vattr.va_type = VCHR;
1219 vattr.va_type = VBLK;
1230 VOP_LEASE(nd.ni_dvp, p, p->p_ucred, LEASE_WRITE);
1232 error = VOP_WHITEOUT(nd.ni_dvp, &nd.ni_cnd, CREATE);
1234 error = VOP_MKNOD(nd.ni_dvp, &nd.ni_vp,
1235 &nd.ni_cnd, &vattr);
1239 NDFREE(&nd, NDF_ONLY_PNBUF);
1242 NDFREE(&nd, NDF_ONLY_PNBUF);
1243 if (nd.ni_dvp == vp)
1250 ASSERT_VOP_UNLOCKED(nd.ni_dvp, "mknod");
1251 ASSERT_VOP_UNLOCKED(nd.ni_vp, "mknod");
1256 * Create a named pipe.
1258 #ifndef _SYS_SYSPROTO_H_
1259 struct mkfifo_args {
1268 register struct mkfifo_args /* {
1269 syscallarg(char *) path;
1270 syscallarg(int) mode;
1275 struct nameidata nd;
1278 NDINIT(&nd, CREATE, LOCKPARENT, UIO_USERSPACE, SCARG(uap, path), p);
1279 if ((error = namei(&nd)) != 0)
1281 if (nd.ni_vp != NULL) {
1282 NDFREE(&nd, NDF_ONLY_PNBUF);
1283 if (nd.ni_dvp == nd.ni_vp)
1291 vattr.va_type = VFIFO;
1292 vattr.va_mode = (SCARG(uap, mode) & ALLPERMS) &~ p->p_fd->fd_cmask;
1293 VOP_LEASE(nd.ni_dvp, p, p->p_ucred, LEASE_WRITE);
1294 error = VOP_MKNOD(nd.ni_dvp, &nd.ni_vp, &nd.ni_cnd, &vattr);
1297 NDFREE(&nd, NDF_ONLY_PNBUF);
1303 * Make a hard file link.
1305 #ifndef _SYS_SYSPROTO_H_
1315 register struct link_args /* {
1316 syscallarg(char *) path;
1317 syscallarg(char *) link;
1320 register struct vnode *vp;
1321 struct nameidata nd;
1325 NDINIT(&nd, LOOKUP, FOLLOW|NOOBJ, UIO_USERSPACE, SCARG(uap, path), p);
1326 if ((error = namei(&nd)) != 0)
1328 NDFREE(&nd, NDF_ONLY_PNBUF);
1330 if (vp->v_type == VDIR)
1331 error = EPERM; /* POSIX */
1333 NDINIT(&nd, CREATE, LOCKPARENT|NOOBJ, UIO_USERSPACE, SCARG(uap, link), p);
1336 if (nd.ni_vp != NULL) {
1341 VOP_LEASE(nd.ni_dvp, p, p->p_ucred,
1343 VOP_LEASE(vp, p, p->p_ucred, LEASE_WRITE);
1344 error = VOP_LINK(nd.ni_dvp, vp, &nd.ni_cnd);
1346 NDFREE(&nd, NDF_ONLY_PNBUF);
1347 if (nd.ni_dvp == nd.ni_vp)
1354 ASSERT_VOP_UNLOCKED(nd.ni_dvp, "link");
1355 ASSERT_VOP_UNLOCKED(nd.ni_vp, "link");
1360 * Make a symbolic link.
1362 #ifndef _SYS_SYSPROTO_H_
1363 struct symlink_args {
1372 register struct symlink_args /* {
1373 syscallarg(char *) path;
1374 syscallarg(char *) link;
1380 struct nameidata nd;
1382 path = zalloc(namei_zone);
1383 if ((error = copyinstr(SCARG(uap, path), path, MAXPATHLEN, NULL)) != 0)
1386 NDINIT(&nd, CREATE, LOCKPARENT|NOOBJ, UIO_USERSPACE, SCARG(uap, link), p);
1387 if ((error = namei(&nd)) != 0)
1390 NDFREE(&nd, NDF_ONLY_PNBUF);
1391 if (nd.ni_dvp == nd.ni_vp)
1400 vattr.va_mode = ACCESSPERMS &~ p->p_fd->fd_cmask;
1401 VOP_LEASE(nd.ni_dvp, p, p->p_ucred, LEASE_WRITE);
1402 error = VOP_SYMLINK(nd.ni_dvp, &nd.ni_vp, &nd.ni_cnd, &vattr, path);
1403 NDFREE(&nd, NDF_ONLY_PNBUF);
1407 ASSERT_VOP_UNLOCKED(nd.ni_dvp, "symlink");
1408 ASSERT_VOP_UNLOCKED(nd.ni_vp, "symlink");
1410 zfree(namei_zone, path);
1415 * Delete a whiteout from the filesystem.
1421 register struct undelete_args /* {
1422 syscallarg(char *) path;
1426 struct nameidata nd;
1429 NDINIT(&nd, DELETE, LOCKPARENT|DOWHITEOUT, UIO_USERSPACE,
1430 SCARG(uap, path), p);
1435 if (nd.ni_vp != NULLVP || !(nd.ni_cnd.cn_flags & ISWHITEOUT)) {
1436 NDFREE(&nd, NDF_ONLY_PNBUF);
1437 if (nd.ni_dvp == nd.ni_vp)
1446 VOP_LEASE(nd.ni_dvp, p, p->p_ucred, LEASE_WRITE);
1447 error = VOP_WHITEOUT(nd.ni_dvp, &nd.ni_cnd, DELETE);
1448 NDFREE(&nd, NDF_ONLY_PNBUF);
1450 ASSERT_VOP_UNLOCKED(nd.ni_dvp, "undelete");
1451 ASSERT_VOP_UNLOCKED(nd.ni_vp, "undelete");
1456 * Delete a name from the filesystem.
1458 #ifndef _SYS_SYSPROTO_H_
1459 struct unlink_args {
1467 struct unlink_args /* {
1468 syscallarg(char *) path;
1471 register struct vnode *vp;
1473 struct nameidata nd;
1476 NDINIT(&nd, DELETE, LOCKPARENT, UIO_USERSPACE, SCARG(uap, path), p);
1477 if ((error = namei(&nd)) != 0)
1480 VOP_LEASE(vp, p, p->p_ucred, LEASE_WRITE);
1481 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY, p);
1483 if (vp->v_type == VDIR)
1484 error = EPERM; /* POSIX */
1487 * The root of a mounted filesystem cannot be deleted.
1489 * XXX: can this only be a VDIR case?
1491 if (vp->v_flag & VROOT)
1496 VOP_LEASE(nd.ni_dvp, p, p->p_ucred, LEASE_WRITE);
1497 error = VOP_REMOVE(nd.ni_dvp, vp, &nd.ni_cnd);
1499 NDFREE(&nd, NDF_ONLY_PNBUF);
1500 if (nd.ni_dvp == vp)
1506 ASSERT_VOP_UNLOCKED(nd.ni_dvp, "unlink");
1507 ASSERT_VOP_UNLOCKED(nd.ni_vp, "unlink");
1512 * Reposition read/write file offset.
1514 #ifndef _SYS_SYSPROTO_H_
1525 register struct lseek_args /* {
1527 syscallarg(int) pad;
1528 syscallarg(off_t) offset;
1529 syscallarg(int) whence;
1532 struct ucred *cred = p->p_ucred;
1533 register struct filedesc *fdp = p->p_fd;
1534 register struct file *fp;
1538 if ((u_int)SCARG(uap, fd) >= fdp->fd_nfiles ||
1539 (fp = fdp->fd_ofiles[SCARG(uap, fd)]) == NULL)
1541 if (fp->f_type != DTYPE_VNODE)
1543 switch (SCARG(uap, whence)) {
1545 fp->f_offset += SCARG(uap, offset);
1548 error=VOP_GETATTR((struct vnode *)fp->f_data, &vattr, cred, p);
1551 fp->f_offset = SCARG(uap, offset) + vattr.va_size;
1554 fp->f_offset = SCARG(uap, offset);
1559 *(off_t *)(p->p_retval) = fp->f_offset;
1563 #if defined(COMPAT_43) || defined(COMPAT_SUNOS)
1565 * Reposition read/write file offset.
1567 #ifndef _SYS_SYSPROTO_H_
1568 struct olseek_args {
1577 register struct olseek_args /* {
1579 syscallarg(long) offset;
1580 syscallarg(int) whence;
1583 struct lseek_args /* {
1585 syscallarg(int) pad;
1586 syscallarg(off_t) offset;
1587 syscallarg(int) whence;
1591 SCARG(&nuap, fd) = SCARG(uap, fd);
1592 SCARG(&nuap, offset) = SCARG(uap, offset);
1593 SCARG(&nuap, whence) = SCARG(uap, whence);
1594 error = lseek(p, &nuap);
1597 #endif /* COMPAT_43 */
1600 * Check access permissions.
1602 #ifndef _SYS_SYSPROTO_H_
1603 struct access_args {
1611 register struct access_args /* {
1612 syscallarg(char *) path;
1613 syscallarg(int) flags;
1616 struct ucred *cred, *tmpcred;
1617 register struct vnode *vp;
1619 struct nameidata nd;
1623 * Create and modify a temporary credential instead of one that
1624 * is potentially shared. This could also mess up socket
1625 * buffer accounting which can run in an interrupt context.
1627 tmpcred = crdup(cred);
1628 tmpcred->cr_uid = p->p_cred->p_ruid;
1629 tmpcred->cr_groups[0] = p->p_cred->p_rgid;
1630 p->p_ucred = tmpcred;
1631 NDINIT(&nd, LOOKUP, FOLLOW | LOCKLEAF | NOOBJ, UIO_USERSPACE,
1632 SCARG(uap, path), p);
1633 if ((error = namei(&nd)) != 0)
1637 /* Flags == 0 means only check for existence. */
1638 if (SCARG(uap, flags)) {
1640 if (SCARG(uap, flags) & R_OK)
1642 if (SCARG(uap, flags) & W_OK)
1644 if (SCARG(uap, flags) & X_OK)
1646 if ((flags & VWRITE) == 0 || (error = vn_writechk(vp)) == 0)
1647 error = VOP_ACCESS(vp, flags, tmpcred, p);
1649 NDFREE(&nd, NDF_ONLY_PNBUF);
1657 #if defined(COMPAT_43) || defined(COMPAT_SUNOS)
1659 * Get file status; this version follows links.
1661 #ifndef _SYS_SYSPROTO_H_
1671 register struct ostat_args /* {
1672 syscallarg(char *) path;
1673 syscallarg(struct ostat *) ub;
1679 struct nameidata nd;
1681 NDINIT(&nd, LOOKUP, FOLLOW | LOCKLEAF | NOOBJ, UIO_USERSPACE,
1682 SCARG(uap, path), p);
1683 if ((error = namei(&nd)) != 0)
1685 NDFREE(&nd, NDF_ONLY_PNBUF);
1686 error = vn_stat(nd.ni_vp, &sb, p);
1691 error = copyout((caddr_t)&osb, (caddr_t)SCARG(uap, ub), sizeof (osb));
1696 * Get file status; this version does not follow links.
1698 #ifndef _SYS_SYSPROTO_H_
1699 struct olstat_args {
1708 register struct olstat_args /* {
1709 syscallarg(char *) path;
1710 syscallarg(struct ostat *) ub;
1717 struct nameidata nd;
1719 NDINIT(&nd, LOOKUP, NOFOLLOW | LOCKLEAF | NOOBJ, UIO_USERSPACE,
1720 SCARG(uap, path), p);
1721 if ((error = namei(&nd)) != 0)
1724 error = vn_stat(vp, &sb, p);
1725 NDFREE(&nd, NDF_ONLY_PNBUF);
1730 error = copyout((caddr_t)&osb, (caddr_t)SCARG(uap, ub), sizeof (osb));
1735 * Convert from an old to a new stat structure.
1743 ost->st_dev = st->st_dev;
1744 ost->st_ino = st->st_ino;
1745 ost->st_mode = st->st_mode;
1746 ost->st_nlink = st->st_nlink;
1747 ost->st_uid = st->st_uid;
1748 ost->st_gid = st->st_gid;
1749 ost->st_rdev = st->st_rdev;
1750 if (st->st_size < (quad_t)1 << 32)
1751 ost->st_size = st->st_size;
1754 ost->st_atime = st->st_atime;
1755 ost->st_mtime = st->st_mtime;
1756 ost->st_ctime = st->st_ctime;
1757 ost->st_blksize = st->st_blksize;
1758 ost->st_blocks = st->st_blocks;
1759 ost->st_flags = st->st_flags;
1760 ost->st_gen = st->st_gen;
1762 #endif /* COMPAT_43 || COMPAT_SUNOS */
1765 * Get file status; this version follows links.
1767 #ifndef _SYS_SYSPROTO_H_
1777 register struct stat_args /* {
1778 syscallarg(char *) path;
1779 syscallarg(struct stat *) ub;
1784 struct nameidata nd;
1786 NDINIT(&nd, LOOKUP, FOLLOW | LOCKLEAF | NOOBJ, UIO_USERSPACE,
1787 SCARG(uap, path), p);
1788 if ((error = namei(&nd)) != 0)
1790 error = vn_stat(nd.ni_vp, &sb, p);
1791 NDFREE(&nd, NDF_ONLY_PNBUF);
1795 error = copyout((caddr_t)&sb, (caddr_t)SCARG(uap, ub), sizeof (sb));
1800 * Get file status; this version does not follow links.
1802 #ifndef _SYS_SYSPROTO_H_
1812 register struct lstat_args /* {
1813 syscallarg(char *) path;
1814 syscallarg(struct stat *) ub;
1820 struct nameidata nd;
1822 NDINIT(&nd, LOOKUP, NOFOLLOW | LOCKLEAF | NOOBJ, UIO_USERSPACE,
1823 SCARG(uap, path), p);
1824 if ((error = namei(&nd)) != 0)
1827 error = vn_stat(vp, &sb, p);
1828 NDFREE(&nd, NDF_ONLY_PNBUF);
1832 error = copyout((caddr_t)&sb, (caddr_t)SCARG(uap, ub), sizeof (sb));
1841 nsb->st_dev = sb->st_dev;
1842 nsb->st_ino = sb->st_ino;
1843 nsb->st_mode = sb->st_mode;
1844 nsb->st_nlink = sb->st_nlink;
1845 nsb->st_uid = sb->st_uid;
1846 nsb->st_gid = sb->st_gid;
1847 nsb->st_rdev = sb->st_rdev;
1848 nsb->st_atimespec = sb->st_atimespec;
1849 nsb->st_mtimespec = sb->st_mtimespec;
1850 nsb->st_ctimespec = sb->st_ctimespec;
1851 nsb->st_size = sb->st_size;
1852 nsb->st_blocks = sb->st_blocks;
1853 nsb->st_blksize = sb->st_blksize;
1854 nsb->st_flags = sb->st_flags;
1855 nsb->st_gen = sb->st_gen;
1856 nsb->st_qspare[0] = sb->st_qspare[0];
1857 nsb->st_qspare[1] = sb->st_qspare[1];
1860 #ifndef _SYS_SYSPROTO_H_
1870 register struct nstat_args /* {
1871 syscallarg(char *) path;
1872 syscallarg(struct nstat *) ub;
1878 struct nameidata nd;
1880 NDINIT(&nd, LOOKUP, FOLLOW | LOCKLEAF | NOOBJ, UIO_USERSPACE,
1881 SCARG(uap, path), p);
1882 if ((error = namei(&nd)) != 0)
1884 NDFREE(&nd, NDF_ONLY_PNBUF);
1885 error = vn_stat(nd.ni_vp, &sb, p);
1889 cvtnstat(&sb, &nsb);
1890 error = copyout((caddr_t)&nsb, (caddr_t)SCARG(uap, ub), sizeof (nsb));
1895 * Get file status; this version does not follow links.
1897 #ifndef _SYS_SYSPROTO_H_
1907 register struct nlstat_args /* {
1908 syscallarg(char *) path;
1909 syscallarg(struct nstat *) ub;
1916 struct nameidata nd;
1918 NDINIT(&nd, LOOKUP, NOFOLLOW | LOCKLEAF | NOOBJ, UIO_USERSPACE,
1919 SCARG(uap, path), p);
1920 if ((error = namei(&nd)) != 0)
1923 NDFREE(&nd, NDF_ONLY_PNBUF);
1924 error = vn_stat(vp, &sb, p);
1928 cvtnstat(&sb, &nsb);
1929 error = copyout((caddr_t)&nsb, (caddr_t)SCARG(uap, ub), sizeof (nsb));
1934 * Get configurable pathname variables.
1936 #ifndef _SYS_SYSPROTO_H_
1937 struct pathconf_args {
1946 register struct pathconf_args /* {
1947 syscallarg(char *) path;
1948 syscallarg(int) name;
1952 struct nameidata nd;
1954 NDINIT(&nd, LOOKUP, FOLLOW | LOCKLEAF | NOOBJ, UIO_USERSPACE,
1955 SCARG(uap, path), p);
1956 if ((error = namei(&nd)) != 0)
1958 NDFREE(&nd, NDF_ONLY_PNBUF);
1959 error = VOP_PATHCONF(nd.ni_vp, SCARG(uap, name), p->p_retval);
1965 * Return target name of a symbolic link.
1967 #ifndef _SYS_SYSPROTO_H_
1968 struct readlink_args {
1978 register struct readlink_args /* {
1979 syscallarg(char *) path;
1980 syscallarg(char *) buf;
1981 syscallarg(int) count;
1984 register struct vnode *vp;
1988 struct nameidata nd;
1990 NDINIT(&nd, LOOKUP, NOFOLLOW | LOCKLEAF | NOOBJ, UIO_USERSPACE,
1991 SCARG(uap, path), p);
1992 if ((error = namei(&nd)) != 0)
1994 NDFREE(&nd, NDF_ONLY_PNBUF);
1996 if (vp->v_type != VLNK)
1999 aiov.iov_base = SCARG(uap, buf);
2000 aiov.iov_len = SCARG(uap, count);
2001 auio.uio_iov = &aiov;
2002 auio.uio_iovcnt = 1;
2003 auio.uio_offset = 0;
2004 auio.uio_rw = UIO_READ;
2005 auio.uio_segflg = UIO_USERSPACE;
2007 auio.uio_resid = SCARG(uap, count);
2008 error = VOP_READLINK(vp, &auio, p->p_ucred);
2011 p->p_retval[0] = SCARG(uap, count) - auio.uio_resid;
2016 setfflags(p, vp, flags)
2025 * Prevent non-root users from setting flags on devices. When
2026 * a device is reused, users can retain ownership of the device
2027 * if they are allowed to set flags and programs assume that
2028 * chown can't fail when done as root.
2030 if ((vp->v_type == VCHR || vp->v_type == VBLK) &&
2031 ((error = suser_xxx(p->p_ucred, p, PRISON_ROOT)) != 0))
2034 VOP_LEASE(vp, p, p->p_ucred, LEASE_WRITE);
2035 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY, p);
2037 vattr.va_flags = flags;
2038 error = VOP_SETATTR(vp, &vattr, p->p_ucred, p);
2039 VOP_UNLOCK(vp, 0, p);
2044 * Change flags of a file given a path name.
2046 #ifndef _SYS_SYSPROTO_H_
2047 struct chflags_args {
2056 register struct chflags_args /* {
2057 syscallarg(char *) path;
2058 syscallarg(int) flags;
2062 struct nameidata nd;
2064 NDINIT(&nd, LOOKUP, FOLLOW, UIO_USERSPACE, SCARG(uap, path), p);
2065 if ((error = namei(&nd)) != 0)
2067 NDFREE(&nd, NDF_ONLY_PNBUF);
2068 error = setfflags(p, nd.ni_vp, SCARG(uap, flags));
2074 * Change flags of a file given a file descriptor.
2076 #ifndef _SYS_SYSPROTO_H_
2077 struct fchflags_args {
2086 register struct fchflags_args /* {
2088 syscallarg(int) flags;
2094 if ((error = getvnode(p->p_fd, SCARG(uap, fd), &fp)) != 0)
2096 return setfflags(p, (struct vnode *) fp->f_data, SCARG(uap, flags));
2100 setfmode(p, vp, mode)
2108 VOP_LEASE(vp, p, p->p_ucred, LEASE_WRITE);
2109 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY, p);
2111 vattr.va_mode = mode & ALLPERMS;
2112 error = VOP_SETATTR(vp, &vattr, p->p_ucred, p);
2113 VOP_UNLOCK(vp, 0, p);
2118 * Change mode of a file given path name.
2120 #ifndef _SYS_SYSPROTO_H_
2130 register struct chmod_args /* {
2131 syscallarg(char *) path;
2132 syscallarg(int) mode;
2136 struct nameidata nd;
2138 NDINIT(&nd, LOOKUP, FOLLOW, UIO_USERSPACE, SCARG(uap, path), p);
2139 if ((error = namei(&nd)) != 0)
2141 NDFREE(&nd, NDF_ONLY_PNBUF);
2142 error = setfmode(p, nd.ni_vp, SCARG(uap, mode));
2148 * Change mode of a file given path name (don't follow links.)
2150 #ifndef _SYS_SYSPROTO_H_
2151 struct lchmod_args {
2160 register struct lchmod_args /* {
2161 syscallarg(char *) path;
2162 syscallarg(int) mode;
2166 struct nameidata nd;
2168 NDINIT(&nd, LOOKUP, NOFOLLOW, UIO_USERSPACE, SCARG(uap, path), p);
2169 if ((error = namei(&nd)) != 0)
2171 NDFREE(&nd, NDF_ONLY_PNBUF);
2172 error = setfmode(p, nd.ni_vp, SCARG(uap, mode));
2178 * Change mode of a file given a file descriptor.
2180 #ifndef _SYS_SYSPROTO_H_
2181 struct fchmod_args {
2190 register struct fchmod_args /* {
2192 syscallarg(int) mode;
2198 if ((error = getvnode(p->p_fd, SCARG(uap, fd), &fp)) != 0)
2200 return setfmode(p, (struct vnode *)fp->f_data, SCARG(uap, mode));
2204 setfown(p, vp, uid, gid)
2213 VOP_LEASE(vp, p, p->p_ucred, LEASE_WRITE);
2214 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY, p);
2218 error = VOP_SETATTR(vp, &vattr, p->p_ucred, p);
2219 VOP_UNLOCK(vp, 0, p);
2224 * Set ownership given a path name.
2226 #ifndef _SYS_SYSPROTO_H_
2237 register struct chown_args /* {
2238 syscallarg(char *) path;
2239 syscallarg(int) uid;
2240 syscallarg(int) gid;
2244 struct nameidata nd;
2246 NDINIT(&nd, LOOKUP, FOLLOW, UIO_USERSPACE, SCARG(uap, path), p);
2247 if ((error = namei(&nd)) != 0)
2249 NDFREE(&nd, NDF_ONLY_PNBUF);
2250 error = setfown(p, nd.ni_vp, SCARG(uap, uid), SCARG(uap, gid));
2256 * Set ownership given a path name, do not cross symlinks.
2258 #ifndef _SYS_SYSPROTO_H_
2259 struct lchown_args {
2269 register struct lchown_args /* {
2270 syscallarg(char *) path;
2271 syscallarg(int) uid;
2272 syscallarg(int) gid;
2276 struct nameidata nd;
2278 NDINIT(&nd, LOOKUP, NOFOLLOW, UIO_USERSPACE, SCARG(uap, path), p);
2279 if ((error = namei(&nd)) != 0)
2281 NDFREE(&nd, NDF_ONLY_PNBUF);
2282 error = setfown(p, nd.ni_vp, SCARG(uap, uid), SCARG(uap, gid));
2288 * Set ownership given a file descriptor.
2290 #ifndef _SYS_SYSPROTO_H_
2291 struct fchown_args {
2301 register struct fchown_args /* {
2303 syscallarg(int) uid;
2304 syscallarg(int) gid;
2310 if ((error = getvnode(p->p_fd, SCARG(uap, fd), &fp)) != 0)
2312 return setfown(p, (struct vnode *)fp->f_data,
2313 SCARG(uap, uid), SCARG(uap, gid));
2317 getutimes(usrtvp, tsp)
2318 const struct timeval *usrtvp;
2319 struct timespec *tsp;
2321 struct timeval tv[2];
2324 if (usrtvp == NULL) {
2326 TIMEVAL_TO_TIMESPEC(&tv[0], &tsp[0]);
2329 if ((error = copyin(usrtvp, tv, sizeof (tv))) != 0)
2331 TIMEVAL_TO_TIMESPEC(&tv[0], &tsp[0]);
2332 TIMEVAL_TO_TIMESPEC(&tv[1], &tsp[1]);
2338 setutimes(p, vp, ts, nullflag)
2341 const struct timespec *ts;
2347 VOP_LEASE(vp, p, p->p_ucred, LEASE_WRITE);
2348 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY, p);
2350 vattr.va_atime = ts[0];
2351 vattr.va_mtime = ts[1];
2353 vattr.va_vaflags |= VA_UTIMES_NULL;
2354 error = VOP_SETATTR(vp, &vattr, p->p_ucred, p);
2355 VOP_UNLOCK(vp, 0, p);
2360 * Set the access and modification times of a file.
2362 #ifndef _SYS_SYSPROTO_H_
2363 struct utimes_args {
2365 struct timeval *tptr;
2372 register struct utimes_args /* {
2373 syscallarg(char *) path;
2374 syscallarg(struct timeval *) tptr;
2377 struct timespec ts[2];
2378 struct timeval *usrtvp;
2380 struct nameidata nd;
2382 usrtvp = SCARG(uap, tptr);
2383 if ((error = getutimes(usrtvp, ts)) != 0)
2385 NDINIT(&nd, LOOKUP, FOLLOW, UIO_USERSPACE, SCARG(uap, path), p);
2386 if ((error = namei(&nd)) != 0)
2388 NDFREE(&nd, NDF_ONLY_PNBUF);
2389 error = setutimes(p, nd.ni_vp, ts, usrtvp == NULL);
2395 * Set the access and modification times of a file.
2397 #ifndef _SYS_SYSPROTO_H_
2398 struct lutimes_args {
2400 struct timeval *tptr;
2407 register struct lutimes_args /* {
2408 syscallarg(char *) path;
2409 syscallarg(struct timeval *) tptr;
2412 struct timespec ts[2];
2413 struct timeval *usrtvp;
2415 struct nameidata nd;
2417 usrtvp = SCARG(uap, tptr);
2418 if ((error = getutimes(usrtvp, ts)) != 0)
2420 NDINIT(&nd, LOOKUP, NOFOLLOW, UIO_USERSPACE, SCARG(uap, path), p);
2421 if ((error = namei(&nd)) != 0)
2423 NDFREE(&nd, NDF_ONLY_PNBUF);
2424 error = setutimes(p, nd.ni_vp, ts, usrtvp == NULL);
2430 * Set the access and modification times of a file.
2432 #ifndef _SYS_SYSPROTO_H_
2433 struct futimes_args {
2435 struct timeval *tptr;
2442 register struct futimes_args /* {
2443 syscallarg(int ) fd;
2444 syscallarg(struct timeval *) tptr;
2447 struct timespec ts[2];
2449 struct timeval *usrtvp;
2452 usrtvp = SCARG(uap, tptr);
2453 if ((error = getutimes(usrtvp, ts)) != 0)
2455 if ((error = getvnode(p->p_fd, SCARG(uap, fd), &fp)) != 0)
2457 return setutimes(p, (struct vnode *)fp->f_data, ts, usrtvp == NULL);
2461 * Truncate a file given its path name.
2463 #ifndef _SYS_SYSPROTO_H_
2464 struct truncate_args {
2474 register struct truncate_args /* {
2475 syscallarg(char *) path;
2476 syscallarg(int) pad;
2477 syscallarg(off_t) length;
2480 register struct vnode *vp;
2483 struct nameidata nd;
2485 if (uap->length < 0)
2487 NDINIT(&nd, LOOKUP, FOLLOW, UIO_USERSPACE, SCARG(uap, path), p);
2488 if ((error = namei(&nd)) != 0)
2491 NDFREE(&nd, NDF_ONLY_PNBUF);
2492 VOP_LEASE(vp, p, p->p_ucred, LEASE_WRITE);
2493 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY, p);
2494 if (vp->v_type == VDIR)
2496 else if ((error = vn_writechk(vp)) == 0 &&
2497 (error = VOP_ACCESS(vp, VWRITE, p->p_ucred, p)) == 0) {
2499 vattr.va_size = SCARG(uap, length);
2500 error = VOP_SETATTR(vp, &vattr, p->p_ucred, p);
2507 * Truncate a file given a file descriptor.
2509 #ifndef _SYS_SYSPROTO_H_
2510 struct ftruncate_args {
2520 register struct ftruncate_args /* {
2522 syscallarg(int) pad;
2523 syscallarg(off_t) length;
2531 if (uap->length < 0)
2533 if ((error = getvnode(p->p_fd, SCARG(uap, fd), &fp)) != 0)
2535 if ((fp->f_flag & FWRITE) == 0)
2537 vp = (struct vnode *)fp->f_data;
2538 VOP_LEASE(vp, p, p->p_ucred, LEASE_WRITE);
2539 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY, p);
2540 if (vp->v_type == VDIR)
2542 else if ((error = vn_writechk(vp)) == 0) {
2544 vattr.va_size = SCARG(uap, length);
2545 error = VOP_SETATTR(vp, &vattr, fp->f_cred, p);
2547 VOP_UNLOCK(vp, 0, p);
2551 #if defined(COMPAT_43) || defined(COMPAT_SUNOS)
2553 * Truncate a file given its path name.
2555 #ifndef _SYS_SYSPROTO_H_
2556 struct otruncate_args {
2565 register struct otruncate_args /* {
2566 syscallarg(char *) path;
2567 syscallarg(long) length;
2570 struct truncate_args /* {
2571 syscallarg(char *) path;
2572 syscallarg(int) pad;
2573 syscallarg(off_t) length;
2576 SCARG(&nuap, path) = SCARG(uap, path);
2577 SCARG(&nuap, length) = SCARG(uap, length);
2578 return (truncate(p, &nuap));
2582 * Truncate a file given a file descriptor.
2584 #ifndef _SYS_SYSPROTO_H_
2585 struct oftruncate_args {
2594 register struct oftruncate_args /* {
2596 syscallarg(long) length;
2599 struct ftruncate_args /* {
2601 syscallarg(int) pad;
2602 syscallarg(off_t) length;
2605 SCARG(&nuap, fd) = SCARG(uap, fd);
2606 SCARG(&nuap, length) = SCARG(uap, length);
2607 return (ftruncate(p, &nuap));
2609 #endif /* COMPAT_43 || COMPAT_SUNOS */
2612 * Sync an open file.
2614 #ifndef _SYS_SYSPROTO_H_
2623 struct fsync_args /* {
2627 register struct vnode *vp;
2632 if ((error = getvnode(p->p_fd, SCARG(uap, fd), &fp)) != 0)
2634 vp = (struct vnode *)fp->f_data;
2635 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY, p);
2636 if (VOP_GETVOBJECT(vp, &obj) == 0)
2637 vm_object_page_clean(obj, 0, 0, 0);
2638 if ((error = VOP_FSYNC(vp, fp->f_cred, MNT_WAIT, p)) == 0 &&
2639 vp->v_mount && (vp->v_mount->mnt_flag & MNT_SOFTDEP) &&
2641 error = (*bioops.io_fsync)(vp);
2642 VOP_UNLOCK(vp, 0, p);
2647 * Rename files. Source and destination must either both be directories,
2648 * or both not be directories. If target is a directory, it must be empty.
2650 #ifndef _SYS_SYSPROTO_H_
2651 struct rename_args {
2660 register struct rename_args /* {
2661 syscallarg(char *) from;
2662 syscallarg(char *) to;
2665 register struct vnode *tvp, *fvp, *tdvp;
2666 struct nameidata fromnd, tond;
2670 NDINIT(&fromnd, DELETE, WANTPARENT | SAVESTART, UIO_USERSPACE,
2671 SCARG(uap, from), p);
2672 if ((error = namei(&fromnd)) != 0)
2675 NDINIT(&tond, RENAME, LOCKPARENT | LOCKLEAF | NOCACHE | SAVESTART | NOOBJ,
2676 UIO_USERSPACE, SCARG(uap, to), p);
2677 if (fromnd.ni_vp->v_type == VDIR)
2678 tond.ni_cnd.cn_flags |= WILLBEDIR;
2679 if ((error = namei(&tond)) != 0) {
2680 /* Translate error code for rename("dir1", "dir2/."). */
2681 if (error == EISDIR && fvp->v_type == VDIR)
2683 NDFREE(&fromnd, NDF_ONLY_PNBUF);
2684 vrele(fromnd.ni_dvp);
2691 if (fvp->v_type == VDIR && tvp->v_type != VDIR) {
2694 } else if (fvp->v_type != VDIR && tvp->v_type == VDIR) {
2702 * If the source is the same as the destination (that is, if they
2703 * are links to the same vnode), then there is nothing to do.
2709 VOP_LEASE(tdvp, p, p->p_ucred, LEASE_WRITE);
2710 if (fromnd.ni_dvp != tdvp) {
2711 VOP_LEASE(fromnd.ni_dvp, p, p->p_ucred, LEASE_WRITE);
2714 VOP_LEASE(tvp, p, p->p_ucred, LEASE_WRITE);
2716 error = VOP_RENAME(fromnd.ni_dvp, fromnd.ni_vp, &fromnd.ni_cnd,
2717 tond.ni_dvp, tond.ni_vp, &tond.ni_cnd);
2718 NDFREE(&fromnd, NDF_ONLY_PNBUF);
2719 NDFREE(&tond, NDF_ONLY_PNBUF);
2721 NDFREE(&fromnd, NDF_ONLY_PNBUF);
2722 NDFREE(&tond, NDF_ONLY_PNBUF);
2729 vrele(fromnd.ni_dvp);
2732 vrele(tond.ni_startdir);
2733 ASSERT_VOP_UNLOCKED(fromnd.ni_dvp, "rename");
2734 ASSERT_VOP_UNLOCKED(fromnd.ni_vp, "rename");
2735 ASSERT_VOP_UNLOCKED(tond.ni_dvp, "rename");
2736 ASSERT_VOP_UNLOCKED(tond.ni_vp, "rename");
2738 if (fromnd.ni_startdir)
2739 vrele(fromnd.ni_startdir);
2746 * Make a directory file.
2748 #ifndef _SYS_SYSPROTO_H_
2758 register struct mkdir_args /* {
2759 syscallarg(char *) path;
2760 syscallarg(int) mode;
2763 register struct vnode *vp;
2766 struct nameidata nd;
2769 NDINIT(&nd, CREATE, LOCKPARENT, UIO_USERSPACE, SCARG(uap, path), p);
2770 nd.ni_cnd.cn_flags |= WILLBEDIR;
2771 if ((error = namei(&nd)) != 0)
2775 NDFREE(&nd, NDF_ONLY_PNBUF);
2776 if (nd.ni_dvp == vp)
2784 vattr.va_type = VDIR;
2785 vattr.va_mode = (SCARG(uap, mode) & ACCESSPERMS) &~ p->p_fd->fd_cmask;
2786 VOP_LEASE(nd.ni_dvp, p, p->p_ucred, LEASE_WRITE);
2787 error = VOP_MKDIR(nd.ni_dvp, &nd.ni_vp, &nd.ni_cnd, &vattr);
2788 NDFREE(&nd, NDF_ONLY_PNBUF);
2792 ASSERT_VOP_UNLOCKED(nd.ni_dvp, "mkdir");
2793 ASSERT_VOP_UNLOCKED(nd.ni_vp, "mkdir");
2798 * Remove a directory file.
2800 #ifndef _SYS_SYSPROTO_H_
2809 struct rmdir_args /* {
2810 syscallarg(char *) path;
2813 register struct vnode *vp;
2815 struct nameidata nd;
2818 NDINIT(&nd, DELETE, LOCKPARENT | LOCKLEAF, UIO_USERSPACE,
2819 SCARG(uap, path), p);
2820 if ((error = namei(&nd)) != 0)
2823 if (vp->v_type != VDIR) {
2828 * No rmdir "." please.
2830 if (nd.ni_dvp == vp) {
2835 * The root of a mounted filesystem cannot be deleted.
2837 if (vp->v_flag & VROOT)
2840 VOP_LEASE(nd.ni_dvp, p, p->p_ucred, LEASE_WRITE);
2841 VOP_LEASE(vp, p, p->p_ucred, LEASE_WRITE);
2842 error = VOP_RMDIR(nd.ni_dvp, nd.ni_vp, &nd.ni_cnd);
2845 NDFREE(&nd, NDF_ONLY_PNBUF);
2846 if (nd.ni_dvp == vp)
2852 ASSERT_VOP_UNLOCKED(nd.ni_dvp, "rmdir");
2853 ASSERT_VOP_UNLOCKED(nd.ni_vp, "rmdir");
2859 * Read a block of directory entries in a file system independent format.
2861 #ifndef _SYS_SYSPROTO_H_
2862 struct ogetdirentries_args {
2870 ogetdirentries(p, uap)
2872 register struct ogetdirentries_args /* {
2874 syscallarg(char *) buf;
2875 syscallarg(u_int) count;
2876 syscallarg(long *) basep;
2881 struct uio auio, kuio;
2882 struct iovec aiov, kiov;
2883 struct dirent *dp, *edp;
2885 int error, eofflag, readcnt;
2888 /* XXX arbitrary sanity limit on `count'. */
2889 if (SCARG(uap, count) > 64 * 1024)
2891 if ((error = getvnode(p->p_fd, SCARG(uap, fd), &fp)) != 0)
2893 if ((fp->f_flag & FREAD) == 0)
2895 vp = (struct vnode *)fp->f_data;
2897 if (vp->v_type != VDIR)
2899 aiov.iov_base = SCARG(uap, buf);
2900 aiov.iov_len = SCARG(uap, count);
2901 auio.uio_iov = &aiov;
2902 auio.uio_iovcnt = 1;
2903 auio.uio_rw = UIO_READ;
2904 auio.uio_segflg = UIO_USERSPACE;
2906 auio.uio_resid = SCARG(uap, count);
2907 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY, p);
2908 loff = auio.uio_offset = fp->f_offset;
2909 # if (BYTE_ORDER != LITTLE_ENDIAN)
2910 if (vp->v_mount->mnt_maxsymlinklen <= 0) {
2911 error = VOP_READDIR(vp, &auio, fp->f_cred, &eofflag,
2913 fp->f_offset = auio.uio_offset;
2918 kuio.uio_iov = &kiov;
2919 kuio.uio_segflg = UIO_SYSSPACE;
2920 kiov.iov_len = SCARG(uap, count);
2921 MALLOC(dirbuf, caddr_t, SCARG(uap, count), M_TEMP, M_WAITOK);
2922 kiov.iov_base = dirbuf;
2923 error = VOP_READDIR(vp, &kuio, fp->f_cred, &eofflag,
2925 fp->f_offset = kuio.uio_offset;
2927 readcnt = SCARG(uap, count) - kuio.uio_resid;
2928 edp = (struct dirent *)&dirbuf[readcnt];
2929 for (dp = (struct dirent *)dirbuf; dp < edp; ) {
2930 # if (BYTE_ORDER == LITTLE_ENDIAN)
2932 * The expected low byte of
2933 * dp->d_namlen is our dp->d_type.
2934 * The high MBZ byte of dp->d_namlen
2935 * is our dp->d_namlen.
2937 dp->d_type = dp->d_namlen;
2941 * The dp->d_type is the high byte
2942 * of the expected dp->d_namlen,
2943 * so must be zero'ed.
2947 if (dp->d_reclen > 0) {
2948 dp = (struct dirent *)
2949 ((char *)dp + dp->d_reclen);
2956 error = uiomove(dirbuf, readcnt, &auio);
2958 FREE(dirbuf, M_TEMP);
2960 VOP_UNLOCK(vp, 0, p);
2963 if (SCARG(uap, count) == auio.uio_resid) {
2964 if (union_dircheckp) {
2965 error = union_dircheckp(p, &vp, fp);
2971 if ((vp->v_flag & VROOT) &&
2972 (vp->v_mount->mnt_flag & MNT_UNION)) {
2973 struct vnode *tvp = vp;
2974 vp = vp->v_mount->mnt_vnodecovered;
2976 fp->f_data = (caddr_t) vp;
2982 error = copyout((caddr_t)&loff, (caddr_t)SCARG(uap, basep),
2984 p->p_retval[0] = SCARG(uap, count) - auio.uio_resid;
2987 #endif /* COMPAT_43 */
2990 * Read a block of directory entries in a file system independent format.
2992 #ifndef _SYS_SYSPROTO_H_
2993 struct getdirentries_args {
3001 getdirentries(p, uap)
3003 register struct getdirentries_args /* {
3005 syscallarg(char *) buf;
3006 syscallarg(u_int) count;
3007 syscallarg(long *) basep;
3017 if ((error = getvnode(p->p_fd, SCARG(uap, fd), &fp)) != 0)
3019 if ((fp->f_flag & FREAD) == 0)
3021 vp = (struct vnode *)fp->f_data;
3023 if (vp->v_type != VDIR)
3025 aiov.iov_base = SCARG(uap, buf);
3026 aiov.iov_len = SCARG(uap, count);
3027 auio.uio_iov = &aiov;
3028 auio.uio_iovcnt = 1;
3029 auio.uio_rw = UIO_READ;
3030 auio.uio_segflg = UIO_USERSPACE;
3032 auio.uio_resid = SCARG(uap, count);
3033 /* vn_lock(vp, LK_SHARED | LK_RETRY, p); */
3034 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY, p);
3035 loff = auio.uio_offset = fp->f_offset;
3036 error = VOP_READDIR(vp, &auio, fp->f_cred, &eofflag, NULL, NULL);
3037 fp->f_offset = auio.uio_offset;
3038 VOP_UNLOCK(vp, 0, p);
3041 if (SCARG(uap, count) == auio.uio_resid) {
3042 if (union_dircheckp) {
3043 error = union_dircheckp(p, &vp, fp);
3049 if ((vp->v_flag & VROOT) &&
3050 (vp->v_mount->mnt_flag & MNT_UNION)) {
3051 struct vnode *tvp = vp;
3052 vp = vp->v_mount->mnt_vnodecovered;
3054 fp->f_data = (caddr_t) vp;
3060 if (SCARG(uap, basep) != NULL) {
3061 error = copyout((caddr_t)&loff, (caddr_t)SCARG(uap, basep),
3064 p->p_retval[0] = SCARG(uap, count) - auio.uio_resid;
3067 #ifndef _SYS_SYSPROTO_H_
3068 struct getdents_args {
3077 register struct getdents_args /* {
3079 syscallarg(char *) buf;
3080 syscallarg(u_int) count;
3083 struct getdirentries_args ap;
3086 ap.count = uap->count;
3088 return getdirentries(p, &ap);
3092 * Set the mode mask for creation of filesystem nodes.
3096 #ifndef _SYS_SYSPROTO_H_
3104 struct umask_args /* {
3105 syscallarg(int) newmask;
3108 register struct filedesc *fdp;
3111 p->p_retval[0] = fdp->fd_cmask;
3112 fdp->fd_cmask = SCARG(uap, newmask) & ALLPERMS;
3117 * Void all references to file by ripping underlying filesystem
3120 #ifndef _SYS_SYSPROTO_H_
3121 struct revoke_args {
3129 register struct revoke_args /* {
3130 syscallarg(char *) path;
3133 register struct vnode *vp;
3136 struct nameidata nd;
3138 NDINIT(&nd, LOOKUP, FOLLOW, UIO_USERSPACE, SCARG(uap, path), p);
3139 if ((error = namei(&nd)) != 0)
3142 NDFREE(&nd, NDF_ONLY_PNBUF);
3143 if (vp->v_type != VCHR && vp->v_type != VBLK) {
3147 if ((error = VOP_GETATTR(vp, &vattr, p->p_ucred, p)) != 0)
3149 if (p->p_ucred->cr_uid != vattr.va_uid &&
3150 (error = suser_xxx(0, p, PRISON_ROOT)))
3153 VOP_REVOKE(vp, REVOKEALL);
3160 * Convert a user file descriptor to a kernel file entry.
3163 getvnode(fdp, fd, fpp)
3164 struct filedesc *fdp;
3170 if ((u_int)fd >= fdp->fd_nfiles ||
3171 (fp = fdp->fd_ofiles[fd]) == NULL)
3173 if (fp->f_type != DTYPE_VNODE && fp->f_type != DTYPE_FIFO)
3179 * Get (NFS) file handle
3181 #ifndef _SYS_SYSPROTO_H_
3190 register struct getfh_args *uap;
3192 struct nameidata nd;
3194 register struct vnode *vp;
3198 * Must be super user
3203 NDINIT(&nd, LOOKUP, FOLLOW | LOCKLEAF, UIO_USERSPACE, uap->fname, p);
3207 NDFREE(&nd, NDF_ONLY_PNBUF);
3209 bzero(&fh, sizeof(fh));
3210 fh.fh_fsid = vp->v_mount->mnt_stat.f_fsid;
3211 error = VFS_VPTOFH(vp, &fh.fh_fid);
3215 error = copyout(&fh, uap->fhp, sizeof (fh));
3220 * syscall for the rpc.lockd to use to translate a NFS file handle into
3221 * an open descriptor.
3223 * warning: do not remove the suser() call or this becomes one giant
3226 #ifndef _SYS_SYSPROTO_H_
3227 struct fhopen_args {
3228 const struct fhandle *u_fhp;
3235 struct fhopen_args /* {
3236 syscallarg(const struct fhandle *) u_fhp;
3237 syscallarg(int) flags;
3244 struct vattr *vap = &vat;
3247 register struct filedesc *fdp = p->p_fd;
3248 int fmode, mode, error, type;
3253 * Must be super user
3259 fmode = FFLAGS(SCARG(uap, flags));
3260 /* why not allow a non-read/write open for our lockd? */
3261 if (((fmode & (FREAD | FWRITE)) == 0) || (fmode & O_CREAT))
3263 error = copyin(SCARG(uap,u_fhp), &fhp, sizeof(fhp));
3266 /* find the mount point */
3267 mp = vfs_getvfs(&fhp.fh_fsid);
3270 /* now give me my vnode, it gets returned to me locked */
3271 error = VFS_FHTOVP(mp, &fhp.fh_fid, &vp);
3275 * from now on we have to make sure not
3276 * to forget about the vnode
3277 * any error that causes an abort must vput(vp)
3278 * just set error = err and 'goto bad;'.
3284 if (vp->v_type == VLNK) {
3288 if (vp->v_type == VSOCK) {
3293 if (fmode & (FWRITE | O_TRUNC)) {
3294 if (vp->v_type == VDIR) {
3298 error = vn_writechk(vp);
3306 error = VOP_ACCESS(vp, mode, p->p_ucred, p);
3310 if (fmode & O_TRUNC) {
3311 VOP_UNLOCK(vp, 0, p); /* XXX */
3312 VOP_LEASE(vp, p, p->p_ucred, LEASE_WRITE);
3313 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY, p); /* XXX */
3316 error = VOP_SETATTR(vp, vap, p->p_ucred, p);
3320 error = VOP_OPEN(vp, fmode, p->p_ucred, p);
3324 * Make sure that a VM object is created for VMIO support.
3326 if (vn_canvmio(vp) == TRUE) {
3327 if ((error = vfs_object_create(vp, p, p->p_ucred)) != 0)
3334 * end of vn_open code
3337 if ((error = falloc(p, &nfp, &indx)) != 0) {
3345 * hold an extra reference to avoid having fp ripped out
3346 * from under us while we block in the lock op.
3349 nfp->f_data = (caddr_t)vp;
3350 nfp->f_flag = fmode & FMASK;
3351 nfp->f_ops = &vnops;
3352 nfp->f_type = DTYPE_VNODE;
3353 if (fmode & (O_EXLOCK | O_SHLOCK)) {
3354 lf.l_whence = SEEK_SET;
3357 if (fmode & O_EXLOCK)
3358 lf.l_type = F_WRLCK;
3360 lf.l_type = F_RDLCK;
3362 if ((fmode & FNONBLOCK) == 0)
3364 VOP_UNLOCK(vp, 0, p);
3365 if ((error = VOP_ADVLOCK(vp, (caddr_t)fp, F_SETLK, &lf, type)) != 0) {
3367 * lock request failed. Normally close the descriptor
3368 * but handle the case where someone might have dup()d
3369 * or close()d it when we weren't looking.
3371 if (fdp->fd_ofiles[indx] == fp) {
3372 fdp->fd_ofiles[indx] = NULL;
3377 * release our private reference.
3382 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY, p);
3383 fp->f_flag |= FHASLOCK;
3385 if ((vp->v_type == VREG) && (VOP_GETVOBJECT(vp, NULL) != 0))
3386 vfs_object_create(vp, p, p->p_ucred);
3388 VOP_UNLOCK(vp, 0, p);
3390 p->p_retval[0] = indx;
3398 #ifndef _SYS_SYSPROTO_H_
3399 struct fhstat_args {
3400 struct fhandle *u_fhp;
3407 register struct fhstat_args /* {
3408 syscallarg(struct fhandle *) u_fhp;
3409 syscallarg(struct stat *) sb;
3419 * Must be super user
3425 error = copyin(SCARG(uap, u_fhp), &fh, sizeof(fhandle_t));
3429 if ((mp = vfs_getvfs(&fh.fh_fsid)) == NULL)
3431 if ((error = VFS_FHTOVP(mp, &fh.fh_fid, &vp)))
3433 error = vn_stat(vp, &sb, p);
3437 error = copyout(&sb, SCARG(uap, sb), sizeof(sb));
3441 #ifndef _SYS_SYSPROTO_H_
3442 struct fhstatfs_args {
3443 struct fhandle *u_fhp;
3450 struct fhstatfs_args /* {
3451 syscallarg(struct fhandle) *u_fhp;
3452 syscallarg(struct statfs) *buf;
3463 * Must be super user
3465 if ((error = suser(p)))
3468 if ((error = copyin(SCARG(uap, u_fhp), &fh, sizeof(fhandle_t))) != 0)
3471 if ((mp = vfs_getvfs(&fh.fh_fsid)) == NULL)
3473 if ((error = VFS_FHTOVP(mp, &fh.fh_fid, &vp)))
3478 if ((error = VFS_STATFS(mp, sp, p)) != 0)
3480 sp->f_flags = mp->mnt_flag & MNT_VISFLAGMASK;
3481 if (suser_xxx(p->p_ucred, 0, 0)) {
3482 bcopy((caddr_t)sp, (caddr_t)&sb, sizeof(sb));
3483 sb.f_fsid.val[0] = sb.f_fsid.val[1] = 0;
3486 return (copyout(sp, SCARG(uap, buf), sizeof(*sp)));
3490 * Syscall to push extended attribute configuration information into the
3491 * VFS. Accepts a path, which it converts to a mountpoint, as well as
3492 * a command (int cmd), and attribute name and misc data. For now, the
3493 * attribute name is left in userspace for consumption by the VFS_op.
3494 * It will probably be changed to be copied into sysspace by the
3495 * syscall in the future, once issues with various consumers of the
3496 * attribute code have raised their hands.
3498 * Currently this is used only by UFS Extended Attributes.
3503 struct extattrctl_args *uap;
3505 struct nameidata nd;
3509 NDINIT(&nd, LOOKUP, FOLLOW, UIO_USERSPACE, SCARG(uap, path), p);
3510 if ((error = namei(&nd)) != 0)
3512 mp = nd.ni_vp->v_mount;
3514 return (VFS_EXTATTRCTL(mp, SCARG(uap, cmd), SCARG(uap, attrname),
3515 SCARG(uap, arg), p));
3519 * Syscall to set a named extended attribute on a file or directory.
3520 * Accepts attribute name, and a uio structure pointing to the data to set.
3521 * The uio is consumed in the style of writev(). The real work happens
3522 * in VOP_SETEXTATTR().
3525 extattr_set_file(p, uap)
3527 struct extattr_set_file_args *uap;
3529 struct nameidata nd;
3531 struct iovec *iov, *needfree = NULL, aiov[UIO_SMALLIOV];
3532 char attrname[EXTATTR_MAXNAMELEN];
3536 error = copyin(SCARG(uap, attrname), attrname, EXTATTR_MAXNAMELEN);
3539 NDINIT(&nd, LOOKUP, FOLLOW | LOCKLEAF, UIO_USERSPACE,
3540 SCARG(uap, path), p);
3541 if ((error = namei(&nd)) != 0)
3543 iovlen = uap->iovcnt * sizeof(struct iovec);
3544 if (uap->iovcnt > UIO_SMALLIOV) {
3545 if (uap->iovcnt > UIO_MAXIOV) {
3549 MALLOC(iov, struct iovec *, iovlen, M_IOV, M_WAITOK);
3554 auio.uio_iovcnt = uap->iovcnt;
3555 auio.uio_rw = UIO_WRITE;
3556 auio.uio_segflg = UIO_USERSPACE;
3558 auio.uio_offset = 0;
3559 if ((error = copyin((caddr_t)uap->iovp, (caddr_t)iov, iovlen)))
3562 for (i = 0; i < uap->iovcnt; i++) {
3563 if (iov->iov_len > INT_MAX - auio.uio_resid) {
3567 auio.uio_resid += iov->iov_len;
3570 cnt = auio.uio_resid;
3571 error = VOP_SETEXTATTR(nd.ni_vp, attrname, &auio, p->p_cred->pc_ucred,
3573 cnt -= auio.uio_resid;
3574 p->p_retval[0] = cnt;
3577 FREE(needfree, M_IOV);
3583 * Syscall to get a named extended attribute on a file or directory.
3584 * Accepts attribute name, and a uio structure pointing to a buffer for the
3585 * data. The uio is consumed in the style of readv(). The real work
3586 * happens in VOP_GETEXTATTR();
3589 extattr_get_file(p, uap)
3591 struct extattr_get_file_args *uap;
3593 struct nameidata nd;
3595 struct iovec *iov, *needfree, aiov[UIO_SMALLIOV];
3596 char attrname[EXTATTR_MAXNAMELEN];
3600 error = copyin(SCARG(uap, attrname), attrname, EXTATTR_MAXNAMELEN);
3603 NDINIT(&nd, LOOKUP, FOLLOW | LOCKLEAF, UIO_USERSPACE,
3604 SCARG(uap, path), p);
3605 if ((error = namei(&nd)) != 0)
3607 iovlen = uap->iovcnt * sizeof (struct iovec);
3608 if (uap->iovcnt > UIO_SMALLIOV) {
3609 if (uap->iovcnt > UIO_MAXIOV) {
3613 MALLOC(iov, struct iovec *, iovlen, M_IOV, M_WAITOK);
3620 auio.uio_iovcnt = uap->iovcnt;
3621 auio.uio_rw = UIO_READ;
3622 auio.uio_segflg = UIO_USERSPACE;
3624 auio.uio_offset = 0;
3625 if ((error = copyin((caddr_t)uap->iovp, (caddr_t)iov, iovlen)))
3628 for (i = 0; i < uap->iovcnt; i++) {
3629 if (iov->iov_len > INT_MAX - auio.uio_resid) {
3633 auio.uio_resid += iov->iov_len;
3636 cnt = auio.uio_resid;
3637 error = VOP_GETEXTATTR(nd.ni_vp, attrname, &auio, p->p_cred->pc_ucred,
3639 cnt -= auio.uio_resid;
3640 p->p_retval[0] = cnt;
3643 FREE(needfree, M_IOV);
3649 * Syscall to delete a named extended attribute from a file or directory.
3650 * Accepts attribute name. The real work happens in VOP_SETEXTATTR().
3653 extattr_delete_file(p, uap)
3655 struct extattr_delete_file_args *uap;
3657 struct nameidata nd;
3658 char attrname[EXTATTR_MAXNAMELEN];
3661 error = copyin(SCARG(uap, attrname), attrname, EXTATTR_MAXNAMELEN);
3664 NDINIT(&nd, LOOKUP, FOLLOW | LOCKLEAF, UIO_USERSPACE,
3665 SCARG(uap, path), p);
3666 if ((error = namei(&nd)) != 0)
3668 error = VOP_SETEXTATTR(nd.ni_vp, attrname, NULL, p->p_cred->pc_ucred,