2 * Copyright (c) 1989, 1993
3 * The Regents of the University of California. All rights reserved.
4 * (c) UNIX System Laboratories, Inc.
5 * All or some portions of this file are derived from material licensed
6 * to the University of California by American Telephone and Telegraph
7 * Co. or Unix System Laboratories, Inc. and are reproduced herein with
8 * the permission of UNIX System Laboratories, Inc.
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
18 * 3. All advertising materials mentioning features or use of this software
19 * must display the following acknowledgement:
20 * This product includes software developed by the University of
21 * California, Berkeley and its contributors.
22 * 4. Neither the name of the University nor the names of its contributors
23 * may be used to endorse or promote products derived from this software
24 * without specific prior written permission.
26 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
27 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
28 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
29 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
30 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
31 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
32 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
33 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
34 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
35 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
38 * @(#)vfs_syscalls.c 8.13 (Berkeley) 4/15/94
39 * $FreeBSD: src/sys/kern/vfs_syscalls.c,v 1.151.2.18 2003/04/04 20:35:58 tegge Exp $
40 * $DragonFly: src/sys/kern/vfs_syscalls.c,v 1.2 2003/06/17 04:28:42 dillon Exp $
43 /* For 4.3 integer FS ID compatibility */
44 #include "opt_compat.h"
46 #include <sys/param.h>
47 #include <sys/systm.h>
49 #include <sys/sysent.h>
50 #include <sys/malloc.h>
51 #include <sys/mount.h>
52 #include <sys/sysproto.h>
53 #include <sys/namei.h>
54 #include <sys/filedesc.h>
55 #include <sys/kernel.h>
56 #include <sys/fcntl.h>
58 #include <sys/linker.h>
60 #include <sys/unistd.h>
61 #include <sys/vnode.h>
63 #include <sys/dirent.h>
64 #include <sys/extattr.h>
66 #include <machine/limits.h>
67 #include <miscfs/union/union.h>
68 #include <sys/sysctl.h>
70 #include <vm/vm_object.h>
71 #include <vm/vm_zone.h>
72 #include <vm/vm_page.h>
74 static int change_dir __P((struct nameidata *ndp, struct proc *p));
75 static void checkdirs __P((struct vnode *olddp));
76 static int chroot_refuse_vdir_fds __P((struct filedesc *fdp));
77 static int getutimes __P((const struct timeval *, struct timespec *));
78 static int setfown __P((struct proc *, struct vnode *, uid_t, gid_t));
79 static int setfmode __P((struct proc *, struct vnode *, int));
80 static int setfflags __P((struct proc *, struct vnode *, int));
81 static int setutimes __P((struct proc *, struct vnode *,
82 const struct timespec *, int));
83 static int usermount = 0; /* if 1, non-root can mount fs. */
85 int (*union_dircheckp) __P((struct proc *, struct vnode **, struct file *));
87 SYSCTL_INT(_vfs, OID_AUTO, usermount, CTLFLAG_RW, &usermount, 0, "");
90 * Virtual File System System Calls
94 * Mount a file system.
96 #ifndef _SYS_SYSPROTO_H_
108 register struct mount_args /* {
109 syscallarg(char *) type;
110 syscallarg(char *) path;
111 syscallarg(int) flags;
112 syscallarg(caddr_t) data;
117 struct vfsconf *vfsp;
118 int error, flag = 0, flag2 = 0;
124 char fstypename[MFSNAMELEN];
126 if (usermount == 0 && (error = suser(p)))
129 * Do not allow NFS export by non-root users.
131 if (SCARG(uap, flags) & MNT_EXPORTED) {
137 * Silently enforce MNT_NOSUID and MNT_NODEV for non-root users
139 if (suser_xxx(p->p_ucred, 0, 0))
140 SCARG(uap, flags) |= MNT_NOSUID | MNT_NODEV;
142 * Get vnode to be covered
144 NDINIT(&nd, LOOKUP, FOLLOW | LOCKLEAF, UIO_USERSPACE,
145 SCARG(uap, path), p);
146 if ((error = namei(&nd)) != 0)
148 NDFREE(&nd, NDF_ONLY_PNBUF);
150 if (SCARG(uap, flags) & MNT_UPDATE) {
151 if ((vp->v_flag & VROOT) == 0) {
157 flag2 = mp->mnt_kern_flag;
159 * We only allow the filesystem to be reloaded if it
160 * is currently mounted read-only.
162 if ((SCARG(uap, flags) & MNT_RELOAD) &&
163 ((mp->mnt_flag & MNT_RDONLY) == 0)) {
165 return (EOPNOTSUPP); /* Needs translation */
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 simple_lock(&vp->v_interlock);
181 if ((vp->v_flag & VMOUNT) != 0 ||
182 vp->v_mountedhere != NULL) {
183 simple_unlock(&vp->v_interlock);
188 vp->v_flag |= VMOUNT;
189 simple_unlock(&vp->v_interlock);
191 SCARG(uap, flags) & (MNT_RELOAD | MNT_FORCE | MNT_UPDATE);
192 VOP_UNLOCK(vp, 0, p);
196 * If the user is not root, ensure that they own the directory
197 * onto which we are attempting to mount.
199 if ((error = VOP_GETATTR(vp, &va, p->p_ucred, p)) ||
200 (va.va_uid != p->p_ucred->cr_uid &&
201 (error = suser(p)))) {
205 if ((error = vinvalbuf(vp, V_SAVE, p->p_ucred, p, 0, 0)) != 0) {
209 if (vp->v_type != VDIR) {
215 * Historically filesystem types were identified by number. If we
216 * get an integer for the filesystem type instead of a string, we
217 * check to see if it matches one of the historic filesystem types.
219 fstypenum = (uintptr_t)SCARG(uap, type);
220 if (fstypenum < maxvfsconf) {
221 for (vfsp = vfsconf; vfsp; vfsp = vfsp->vfc_next)
222 if (vfsp->vfc_typenum == fstypenum)
228 strncpy(fstypename, vfsp->vfc_name, MFSNAMELEN);
230 #endif /* COMPAT_43 */
231 if ((error = copyinstr(SCARG(uap, type), fstypename, MFSNAMELEN, NULL)) != 0) {
235 for (vfsp = vfsconf; vfsp; vfsp = vfsp->vfc_next)
236 if (!strcmp(vfsp->vfc_name, fstypename))
241 /* Only load modules for root (very important!) */
242 if ((error = suser(p)) != 0) {
246 error = linker_load_file(fstypename, &lf);
247 if (error || lf == NULL) {
254 /* lookup again, see if the VFS was loaded */
255 for (vfsp = vfsconf; vfsp; vfsp = vfsp->vfc_next)
256 if (!strcmp(vfsp->vfc_name, fstypename))
260 linker_file_unload(lf);
265 simple_lock(&vp->v_interlock);
266 if ((vp->v_flag & VMOUNT) != 0 ||
267 vp->v_mountedhere != NULL) {
268 simple_unlock(&vp->v_interlock);
272 vp->v_flag |= VMOUNT;
273 simple_unlock(&vp->v_interlock);
276 * Allocate and initialize the filesystem.
278 mp = malloc(sizeof(struct mount), M_MOUNT, M_WAITOK);
279 bzero((char *)mp, (u_long)sizeof(struct mount));
280 TAILQ_INIT(&mp->mnt_nvnodelist);
281 TAILQ_INIT(&mp->mnt_reservedvnlist);
282 mp->mnt_nvnodelistsize = 0;
283 lockinit(&mp->mnt_lock, PVFS, "vfslock", 0, LK_NOPAUSE);
284 (void)vfs_busy(mp, LK_NOWAIT, 0, p);
285 mp->mnt_op = vfsp->vfc_vfsops;
287 vfsp->vfc_refcount++;
288 mp->mnt_stat.f_type = vfsp->vfc_typenum;
289 mp->mnt_flag |= vfsp->vfc_flags & MNT_VISFLAGMASK;
290 strncpy(mp->mnt_stat.f_fstypename, vfsp->vfc_name, MFSNAMELEN);
291 mp->mnt_vnodecovered = vp;
292 mp->mnt_stat.f_owner = p->p_ucred->cr_uid;
293 mp->mnt_iosize_max = DFLTPHYS;
294 VOP_UNLOCK(vp, 0, p);
297 * Set the mount level flags.
299 if (SCARG(uap, flags) & MNT_RDONLY)
300 mp->mnt_flag |= MNT_RDONLY;
301 else if (mp->mnt_flag & MNT_RDONLY)
302 mp->mnt_kern_flag |= MNTK_WANTRDWR;
303 mp->mnt_flag &=~ (MNT_NOSUID | MNT_NOEXEC | MNT_NODEV |
304 MNT_SYNCHRONOUS | MNT_UNION | MNT_ASYNC | MNT_NOATIME |
305 MNT_NOSYMFOLLOW | MNT_IGNORE |
306 MNT_NOCLUSTERR | MNT_NOCLUSTERW | MNT_SUIDDIR);
307 mp->mnt_flag |= SCARG(uap, flags) & (MNT_NOSUID | MNT_NOEXEC |
308 MNT_NODEV | MNT_SYNCHRONOUS | MNT_UNION | MNT_ASYNC | MNT_FORCE |
309 MNT_NOSYMFOLLOW | MNT_IGNORE |
310 MNT_NOATIME | MNT_NOCLUSTERR | MNT_NOCLUSTERW | MNT_SUIDDIR);
312 * Mount the filesystem.
313 * XXX The final recipients of VFS_MOUNT just overwrite the ndp they
314 * get. No freeing of cn_pnbuf.
316 error = VFS_MOUNT(mp, SCARG(uap, path), SCARG(uap, data), &nd, p);
317 if (mp->mnt_flag & MNT_UPDATE) {
318 if (mp->mnt_kern_flag & MNTK_WANTRDWR)
319 mp->mnt_flag &= ~MNT_RDONLY;
320 mp->mnt_flag &=~ (MNT_UPDATE | MNT_RELOAD | MNT_FORCE);
321 mp->mnt_kern_flag &=~ MNTK_WANTRDWR;
324 mp->mnt_kern_flag = flag2;
326 if ((mp->mnt_flag & MNT_RDONLY) == 0) {
327 if (mp->mnt_syncer == NULL)
328 error = vfs_allocate_syncvnode(mp);
330 if (mp->mnt_syncer != NULL)
331 vrele(mp->mnt_syncer);
332 mp->mnt_syncer = NULL;
335 simple_lock(&vp->v_interlock);
336 vp->v_flag &= ~VMOUNT;
337 simple_unlock(&vp->v_interlock);
341 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY, p);
343 * Put the new filesystem on the mount list after root.
347 simple_lock(&vp->v_interlock);
348 vp->v_flag &= ~VMOUNT;
349 vp->v_mountedhere = mp;
350 simple_unlock(&vp->v_interlock);
351 simple_lock(&mountlist_slock);
352 TAILQ_INSERT_TAIL(&mountlist, mp, mnt_list);
353 simple_unlock(&mountlist_slock);
355 VOP_UNLOCK(vp, 0, p);
356 if ((mp->mnt_flag & MNT_RDONLY) == 0)
357 error = vfs_allocate_syncvnode(mp);
359 if ((error = VFS_START(mp, 0, p)) != 0)
362 simple_lock(&vp->v_interlock);
363 vp->v_flag &= ~VMOUNT;
364 simple_unlock(&vp->v_interlock);
365 mp->mnt_vfc->vfc_refcount--;
367 free((caddr_t)mp, M_MOUNT);
374 * Scan all active processes to see if any of them have a current
375 * or root directory onto which the new filesystem has just been
376 * mounted. If so, replace them with the new mount point.
382 struct filedesc *fdp;
386 if (olddp->v_usecount == 1)
388 if (VFS_ROOT(olddp->v_mountedhere, &newdp))
389 panic("mount: lost mount");
390 LIST_FOREACH(p, &allproc, p_list) {
392 if (fdp->fd_cdir == olddp) {
395 fdp->fd_cdir = newdp;
397 if (fdp->fd_rdir == olddp) {
400 fdp->fd_rdir = newdp;
403 if (rootvnode == olddp) {
412 * Unmount a file system.
414 * Note: unmount takes a path to the vnode mounted on as argument,
415 * not special file (as before).
417 #ifndef _SYS_SYSPROTO_H_
418 struct unmount_args {
427 register struct unmount_args /* {
428 syscallarg(char *) path;
429 syscallarg(int) flags;
432 register struct vnode *vp;
437 NDINIT(&nd, LOOKUP, FOLLOW | LOCKLEAF, UIO_USERSPACE,
438 SCARG(uap, path), p);
439 if ((error = namei(&nd)) != 0)
442 NDFREE(&nd, NDF_ONLY_PNBUF);
446 * Only root, or the user that did the original mount is
447 * permitted to unmount this filesystem.
449 if ((mp->mnt_stat.f_owner != p->p_ucred->cr_uid) &&
450 (error = suser(p))) {
456 * Don't allow unmounting the root file system.
458 if (mp->mnt_flag & MNT_ROOTFS) {
464 * Must be the root of the filesystem
466 if ((vp->v_flag & VROOT) == 0) {
471 return (dounmount(mp, SCARG(uap, flags), p));
475 * Do the actual file system unmount.
478 dounmount(mp, flags, p)
479 register struct mount *mp;
483 struct vnode *coveredvp;
487 simple_lock(&mountlist_slock);
488 if (mp->mnt_kern_flag & MNTK_UNMOUNT) {
489 simple_unlock(&mountlist_slock);
492 mp->mnt_kern_flag |= MNTK_UNMOUNT;
493 /* Allow filesystems to detect that a forced unmount is in progress. */
494 if (flags & MNT_FORCE)
495 mp->mnt_kern_flag |= MNTK_UNMOUNTF;
496 error = lockmgr(&mp->mnt_lock, LK_DRAIN | LK_INTERLOCK |
497 ((flags & MNT_FORCE) ? 0 : LK_NOWAIT), &mountlist_slock, p);
499 mp->mnt_kern_flag &= ~(MNTK_UNMOUNT | MNTK_UNMOUNTF);
500 if (mp->mnt_kern_flag & MNTK_MWAIT)
505 if (mp->mnt_flag & MNT_EXPUBLIC)
506 vfs_setpublicfs(NULL, NULL, NULL);
508 vfs_msync(mp, MNT_WAIT);
509 async_flag = mp->mnt_flag & MNT_ASYNC;
510 mp->mnt_flag &=~ MNT_ASYNC;
511 cache_purgevfs(mp); /* remove cache entries for this file sys */
512 if (mp->mnt_syncer != NULL)
513 vrele(mp->mnt_syncer);
514 if (((mp->mnt_flag & MNT_RDONLY) ||
515 (error = VFS_SYNC(mp, MNT_WAIT, p->p_ucred, p)) == 0) ||
517 error = VFS_UNMOUNT(mp, flags, p);
518 simple_lock(&mountlist_slock);
520 if ((mp->mnt_flag & MNT_RDONLY) == 0 && mp->mnt_syncer == NULL)
521 (void) vfs_allocate_syncvnode(mp);
522 mp->mnt_kern_flag &= ~(MNTK_UNMOUNT | MNTK_UNMOUNTF);
523 mp->mnt_flag |= async_flag;
524 lockmgr(&mp->mnt_lock, LK_RELEASE | LK_INTERLOCK | LK_REENABLE,
525 &mountlist_slock, p);
526 if (mp->mnt_kern_flag & MNTK_MWAIT)
530 TAILQ_REMOVE(&mountlist, mp, mnt_list);
531 if ((coveredvp = mp->mnt_vnodecovered) != NULLVP) {
532 coveredvp->v_mountedhere = (struct mount *)0;
535 mp->mnt_vfc->vfc_refcount--;
536 if (!TAILQ_EMPTY(&mp->mnt_nvnodelist))
537 panic("unmount: dangling vnode");
538 lockmgr(&mp->mnt_lock, LK_RELEASE | LK_INTERLOCK, &mountlist_slock, p);
539 if (mp->mnt_kern_flag & MNTK_MWAIT)
541 free((caddr_t)mp, M_MOUNT);
546 * Sync each mounted filesystem.
548 #ifndef _SYS_SYSPROTO_H_
555 static int syncprt = 0;
556 SYSCTL_INT(_debug, OID_AUTO, syncprt, CTLFLAG_RW, &syncprt, 0, "");
563 struct sync_args *uap;
565 register struct mount *mp, *nmp;
568 simple_lock(&mountlist_slock);
569 for (mp = TAILQ_FIRST(&mountlist); mp != NULL; mp = nmp) {
570 if (vfs_busy(mp, LK_NOWAIT, &mountlist_slock, p)) {
571 nmp = TAILQ_NEXT(mp, mnt_list);
574 if ((mp->mnt_flag & MNT_RDONLY) == 0) {
575 asyncflag = mp->mnt_flag & MNT_ASYNC;
576 mp->mnt_flag &= ~MNT_ASYNC;
577 vfs_msync(mp, MNT_NOWAIT);
578 VFS_SYNC(mp, MNT_NOWAIT,
579 ((p != NULL) ? p->p_ucred : NOCRED), p);
580 mp->mnt_flag |= asyncflag;
582 simple_lock(&mountlist_slock);
583 nmp = TAILQ_NEXT(mp, mnt_list);
586 simple_unlock(&mountlist_slock);
589 * XXX don't call vfs_bufstats() yet because that routine
590 * was not imported in the Lite2 merge.
595 #endif /* DIAGNOSTIC */
600 /* XXX PRISON: could be per prison flag */
601 static int prison_quotas;
603 SYSCTL_INT(_kern_prison, OID_AUTO, quotas, CTLFLAG_RW, &prison_quotas, 0, "");
607 * Change filesystem quotas.
609 #ifndef _SYS_SYSPROTO_H_
610 struct quotactl_args {
621 register struct quotactl_args /* {
622 syscallarg(char *) path;
625 syscallarg(caddr_t) arg;
628 register struct mount *mp;
632 if (p->p_prison && !prison_quotas)
634 NDINIT(&nd, LOOKUP, FOLLOW, UIO_USERSPACE, SCARG(uap, path), p);
635 if ((error = namei(&nd)) != 0)
637 mp = nd.ni_vp->v_mount;
638 NDFREE(&nd, NDF_ONLY_PNBUF);
640 return (VFS_QUOTACTL(mp, SCARG(uap, cmd), SCARG(uap, uid),
641 SCARG(uap, arg), p));
645 * Get filesystem statistics.
647 #ifndef _SYS_SYSPROTO_H_
657 register struct statfs_args /* {
658 syscallarg(char *) path;
659 syscallarg(struct statfs *) buf;
662 register struct mount *mp;
663 register struct statfs *sp;
668 NDINIT(&nd, LOOKUP, FOLLOW, UIO_USERSPACE, SCARG(uap, path), p);
669 if ((error = namei(&nd)) != 0)
671 mp = nd.ni_vp->v_mount;
673 NDFREE(&nd, NDF_ONLY_PNBUF);
675 error = VFS_STATFS(mp, sp, p);
678 sp->f_flags = mp->mnt_flag & MNT_VISFLAGMASK;
679 if (suser_xxx(p->p_ucred, 0, 0)) {
680 bcopy((caddr_t)sp, (caddr_t)&sb, sizeof(sb));
681 sb.f_fsid.val[0] = sb.f_fsid.val[1] = 0;
684 return (copyout((caddr_t)sp, (caddr_t)SCARG(uap, buf), sizeof(*sp)));
688 * Get filesystem statistics.
690 #ifndef _SYS_SYSPROTO_H_
691 struct fstatfs_args {
700 register struct fstatfs_args /* {
702 syscallarg(struct statfs *) buf;
707 register struct statfs *sp;
711 if ((error = getvnode(p->p_fd, SCARG(uap, fd), &fp)) != 0)
713 mp = ((struct vnode *)fp->f_data)->v_mount;
717 error = VFS_STATFS(mp, sp, p);
720 sp->f_flags = mp->mnt_flag & MNT_VISFLAGMASK;
721 if (suser_xxx(p->p_ucred, 0, 0)) {
722 bcopy((caddr_t)sp, (caddr_t)&sb, sizeof(sb));
723 sb.f_fsid.val[0] = sb.f_fsid.val[1] = 0;
726 return (copyout((caddr_t)sp, (caddr_t)SCARG(uap, buf), sizeof(*sp)));
730 * Get statistics on all filesystems.
732 #ifndef _SYS_SYSPROTO_H_
733 struct getfsstat_args {
742 register struct getfsstat_args /* {
743 syscallarg(struct statfs *) buf;
744 syscallarg(long) bufsize;
745 syscallarg(int) flags;
748 register struct mount *mp, *nmp;
749 register struct statfs *sp;
751 long count, maxcount, error;
753 maxcount = SCARG(uap, bufsize) / sizeof(struct statfs);
754 sfsp = (caddr_t)SCARG(uap, buf);
756 simple_lock(&mountlist_slock);
757 for (mp = TAILQ_FIRST(&mountlist); mp != NULL; mp = nmp) {
758 if (vfs_busy(mp, LK_NOWAIT, &mountlist_slock, p)) {
759 nmp = TAILQ_NEXT(mp, mnt_list);
762 if (sfsp && count < maxcount) {
765 * If MNT_NOWAIT or MNT_LAZY is specified, do not
766 * refresh the fsstat cache. MNT_NOWAIT or MNT_LAZY
767 * overrides MNT_WAIT.
769 if (((SCARG(uap, flags) & (MNT_LAZY|MNT_NOWAIT)) == 0 ||
770 (SCARG(uap, flags) & MNT_WAIT)) &&
771 (error = VFS_STATFS(mp, sp, p))) {
772 simple_lock(&mountlist_slock);
773 nmp = TAILQ_NEXT(mp, mnt_list);
777 sp->f_flags = mp->mnt_flag & MNT_VISFLAGMASK;
778 error = copyout((caddr_t)sp, sfsp, sizeof(*sp));
786 simple_lock(&mountlist_slock);
787 nmp = TAILQ_NEXT(mp, mnt_list);
790 simple_unlock(&mountlist_slock);
791 if (sfsp && count > maxcount)
792 p->p_retval[0] = maxcount;
794 p->p_retval[0] = count;
799 * Change current working directory to a given file descriptor.
801 #ifndef _SYS_SYSPROTO_H_
810 struct fchdir_args /* {
814 register struct filedesc *fdp = p->p_fd;
815 struct vnode *vp, *tdp;
820 if ((error = getvnode(fdp, SCARG(uap, fd), &fp)) != 0)
822 vp = (struct vnode *)fp->f_data;
824 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY, p);
825 if (vp->v_type != VDIR)
828 error = VOP_ACCESS(vp, VEXEC, p->p_ucred, p);
829 while (!error && (mp = vp->v_mountedhere) != NULL) {
830 if (vfs_busy(mp, 0, 0, p))
832 error = VFS_ROOT(mp, &tdp);
843 VOP_UNLOCK(vp, 0, p);
850 * Change current working directory (``.'').
852 #ifndef _SYS_SYSPROTO_H_
861 struct chdir_args /* {
862 syscallarg(char *) path;
865 register struct filedesc *fdp = p->p_fd;
869 NDINIT(&nd, LOOKUP, FOLLOW | LOCKLEAF, UIO_USERSPACE,
870 SCARG(uap, path), p);
871 if ((error = change_dir(&nd, p)) != 0)
873 NDFREE(&nd, NDF_ONLY_PNBUF);
875 fdp->fd_cdir = nd.ni_vp;
880 * Helper function for raised chroot(2) security function: Refuse if
881 * any filedescriptors are open directories.
884 chroot_refuse_vdir_fds(fdp)
885 struct filedesc *fdp;
892 for (fd = 0; fd < fdp->fd_nfiles ; fd++) {
893 error = getvnode(fdp, fd, &fp);
896 vp = (struct vnode *)fp->f_data;
897 if (vp->v_type != VDIR)
905 * This sysctl determines if we will allow a process to chroot(2) if it
906 * has a directory open:
907 * 0: disallowed for all processes.
908 * 1: allowed for processes that were not already chroot(2)'ed.
909 * 2: allowed for all processes.
912 static int chroot_allow_open_directories = 1;
914 SYSCTL_INT(_kern, OID_AUTO, chroot_allow_open_directories, CTLFLAG_RW,
915 &chroot_allow_open_directories, 0, "");
918 * Change notion of root (``/'') directory.
920 #ifndef _SYS_SYSPROTO_H_
929 struct chroot_args /* {
930 syscallarg(char *) path;
933 register struct filedesc *fdp = p->p_fd;
937 error = suser_xxx(0, p, PRISON_ROOT);
940 if (chroot_allow_open_directories == 0 ||
941 (chroot_allow_open_directories == 1 && fdp->fd_rdir != rootvnode))
942 error = chroot_refuse_vdir_fds(fdp);
945 NDINIT(&nd, LOOKUP, FOLLOW | LOCKLEAF, UIO_USERSPACE,
946 SCARG(uap, path), p);
947 if ((error = change_dir(&nd, p)) != 0)
949 NDFREE(&nd, NDF_ONLY_PNBUF);
951 fdp->fd_rdir = nd.ni_vp;
953 fdp->fd_jdir = nd.ni_vp;
960 * Common routine for chroot and chdir.
964 register struct nameidata *ndp;
974 if (vp->v_type != VDIR)
977 error = VOP_ACCESS(vp, VEXEC, p->p_ucred, p);
981 VOP_UNLOCK(vp, 0, p);
986 * Check permissions, allocate an open file structure,
987 * and call the device open routine if any.
989 #ifndef _SYS_SYSPROTO_H_
999 register struct open_args /* {
1000 syscallarg(char *) path;
1001 syscallarg(int) flags;
1002 syscallarg(int) mode;
1005 register struct filedesc *fdp = p->p_fd;
1006 register struct file *fp;
1007 register struct vnode *vp;
1008 int cmode, flags, oflags;
1010 int type, indx, error;
1012 struct nameidata nd;
1014 oflags = SCARG(uap, flags);
1015 if ((oflags & O_ACCMODE) == O_ACCMODE)
1017 flags = FFLAGS(oflags);
1018 error = falloc(p, &nfp, &indx);
1022 cmode = ((SCARG(uap, mode) &~ fdp->fd_cmask) & ALLPERMS) &~ S_ISTXT;
1023 NDINIT(&nd, LOOKUP, FOLLOW, UIO_USERSPACE, SCARG(uap, path), p);
1024 p->p_dupfd = -indx - 1; /* XXX check for fdopen */
1026 * Bump the ref count to prevent another process from closing
1027 * the descriptor while we are blocked in vn_open()
1030 error = vn_open(&nd, flags, cmode);
1033 * release our own reference
1038 * handle special fdopen() case. bleh. dupfdopen() is
1039 * responsible for dropping the old contents of ofiles[indx]
1042 if ((error == ENODEV || error == ENXIO) &&
1043 p->p_dupfd >= 0 && /* XXX from fdopen */
1045 dupfdopen(p, fdp, indx, p->p_dupfd, flags, error)) == 0) {
1046 p->p_retval[0] = indx;
1050 * Clean up the descriptor, but only if another thread hadn't
1051 * replaced or closed it.
1053 if (fdp->fd_ofiles[indx] == fp) {
1054 fdp->fd_ofiles[indx] = NULL;
1058 if (error == ERESTART)
1063 NDFREE(&nd, NDF_ONLY_PNBUF);
1067 * There should be 2 references on the file, one from the descriptor
1068 * table, and one for us.
1070 * Handle the case where someone closed the file (via its file
1071 * descriptor) while we were blocked. The end result should look
1072 * like opening the file succeeded but it was immediately closed.
1074 if (fp->f_count == 1) {
1075 KASSERT(fdp->fd_ofiles[indx] != fp,
1076 ("Open file descriptor lost all refs"));
1077 VOP_UNLOCK(vp, 0, p);
1078 vn_close(vp, flags & FMASK, fp->f_cred, p);
1080 p->p_retval[0] = indx;
1084 fp->f_data = (caddr_t)vp;
1085 fp->f_flag = flags & FMASK;
1087 fp->f_type = (vp->v_type == VFIFO ? DTYPE_FIFO : DTYPE_VNODE);
1088 if (flags & (O_EXLOCK | O_SHLOCK)) {
1089 lf.l_whence = SEEK_SET;
1092 if (flags & O_EXLOCK)
1093 lf.l_type = F_WRLCK;
1095 lf.l_type = F_RDLCK;
1097 if ((flags & FNONBLOCK) == 0)
1099 VOP_UNLOCK(vp, 0, p);
1100 if ((error = VOP_ADVLOCK(vp, (caddr_t)fp, F_SETLK, &lf, type)) != 0) {
1102 * lock request failed. Normally close the descriptor
1103 * but handle the case where someone might have dup()d
1104 * it when we weren't looking. One reference is
1105 * owned by the descriptor array, the other by us.
1107 if (fdp->fd_ofiles[indx] == fp) {
1108 fdp->fd_ofiles[indx] = NULL;
1114 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY, p);
1115 fp->f_flag |= FHASLOCK;
1117 /* assert that vn_open created a backing object if one is needed */
1118 KASSERT(!vn_canvmio(vp) || VOP_GETVOBJECT(vp, NULL) == 0,
1119 ("open: vmio vnode has no backing object after vn_open"));
1120 VOP_UNLOCK(vp, 0, p);
1123 * release our private reference, leaving the one associated with the
1124 * descriptor table intact.
1127 p->p_retval[0] = indx;
1135 #ifndef _SYS_SYSPROTO_H_
1136 struct ocreat_args {
1144 register struct ocreat_args /* {
1145 syscallarg(char *) path;
1146 syscallarg(int) mode;
1149 struct open_args /* {
1150 syscallarg(char *) path;
1151 syscallarg(int) flags;
1152 syscallarg(int) mode;
1155 SCARG(&nuap, path) = SCARG(uap, path);
1156 SCARG(&nuap, mode) = SCARG(uap, mode);
1157 SCARG(&nuap, flags) = O_WRONLY | O_CREAT | O_TRUNC;
1158 return (open(p, &nuap));
1160 #endif /* COMPAT_43 */
1163 * Create a special file.
1165 #ifndef _SYS_SYSPROTO_H_
1176 register struct mknod_args /* {
1177 syscallarg(char *) path;
1178 syscallarg(int) mode;
1179 syscallarg(int) dev;
1182 register struct vnode *vp;
1186 struct nameidata nd;
1188 switch (SCARG(uap, mode) & S_IFMT) {
1194 error = suser_xxx(0, p, PRISON_ROOT);
1200 NDINIT(&nd, CREATE, LOCKPARENT, UIO_USERSPACE, SCARG(uap, path), p);
1201 if ((error = namei(&nd)) != 0)
1208 vattr.va_mode = (SCARG(uap, mode) & ALLPERMS) &~ p->p_fd->fd_cmask;
1209 vattr.va_rdev = SCARG(uap, dev);
1212 switch (SCARG(uap, mode) & S_IFMT) {
1213 case S_IFMT: /* used by badsect to flag bad sectors */
1214 vattr.va_type = VBAD;
1217 vattr.va_type = VCHR;
1220 vattr.va_type = VBLK;
1231 VOP_LEASE(nd.ni_dvp, p, p->p_ucred, LEASE_WRITE);
1233 error = VOP_WHITEOUT(nd.ni_dvp, &nd.ni_cnd, CREATE);
1235 error = VOP_MKNOD(nd.ni_dvp, &nd.ni_vp,
1236 &nd.ni_cnd, &vattr);
1240 NDFREE(&nd, NDF_ONLY_PNBUF);
1243 NDFREE(&nd, NDF_ONLY_PNBUF);
1244 if (nd.ni_dvp == vp)
1251 ASSERT_VOP_UNLOCKED(nd.ni_dvp, "mknod");
1252 ASSERT_VOP_UNLOCKED(nd.ni_vp, "mknod");
1257 * Create a named pipe.
1259 #ifndef _SYS_SYSPROTO_H_
1260 struct mkfifo_args {
1269 register struct mkfifo_args /* {
1270 syscallarg(char *) path;
1271 syscallarg(int) mode;
1276 struct nameidata nd;
1279 NDINIT(&nd, CREATE, LOCKPARENT, UIO_USERSPACE, SCARG(uap, path), p);
1280 if ((error = namei(&nd)) != 0)
1282 if (nd.ni_vp != NULL) {
1283 NDFREE(&nd, NDF_ONLY_PNBUF);
1284 if (nd.ni_dvp == nd.ni_vp)
1292 vattr.va_type = VFIFO;
1293 vattr.va_mode = (SCARG(uap, mode) & ALLPERMS) &~ p->p_fd->fd_cmask;
1294 VOP_LEASE(nd.ni_dvp, p, p->p_ucred, LEASE_WRITE);
1295 error = VOP_MKNOD(nd.ni_dvp, &nd.ni_vp, &nd.ni_cnd, &vattr);
1298 NDFREE(&nd, NDF_ONLY_PNBUF);
1304 * Make a hard file link.
1306 #ifndef _SYS_SYSPROTO_H_
1316 register struct link_args /* {
1317 syscallarg(char *) path;
1318 syscallarg(char *) link;
1321 register struct vnode *vp;
1322 struct nameidata nd;
1326 NDINIT(&nd, LOOKUP, FOLLOW|NOOBJ, UIO_USERSPACE, SCARG(uap, path), p);
1327 if ((error = namei(&nd)) != 0)
1329 NDFREE(&nd, NDF_ONLY_PNBUF);
1331 if (vp->v_type == VDIR)
1332 error = EPERM; /* POSIX */
1334 NDINIT(&nd, CREATE, LOCKPARENT|NOOBJ, UIO_USERSPACE, SCARG(uap, link), p);
1337 if (nd.ni_vp != NULL) {
1342 VOP_LEASE(nd.ni_dvp, p, p->p_ucred,
1344 VOP_LEASE(vp, p, p->p_ucred, LEASE_WRITE);
1345 error = VOP_LINK(nd.ni_dvp, vp, &nd.ni_cnd);
1347 NDFREE(&nd, NDF_ONLY_PNBUF);
1348 if (nd.ni_dvp == nd.ni_vp)
1355 ASSERT_VOP_UNLOCKED(nd.ni_dvp, "link");
1356 ASSERT_VOP_UNLOCKED(nd.ni_vp, "link");
1361 * Make a symbolic link.
1363 #ifndef _SYS_SYSPROTO_H_
1364 struct symlink_args {
1373 register struct symlink_args /* {
1374 syscallarg(char *) path;
1375 syscallarg(char *) link;
1381 struct nameidata nd;
1383 path = zalloc(namei_zone);
1384 if ((error = copyinstr(SCARG(uap, path), path, MAXPATHLEN, NULL)) != 0)
1387 NDINIT(&nd, CREATE, LOCKPARENT|NOOBJ, UIO_USERSPACE, SCARG(uap, link), p);
1388 if ((error = namei(&nd)) != 0)
1391 NDFREE(&nd, NDF_ONLY_PNBUF);
1392 if (nd.ni_dvp == nd.ni_vp)
1401 vattr.va_mode = ACCESSPERMS &~ p->p_fd->fd_cmask;
1402 VOP_LEASE(nd.ni_dvp, p, p->p_ucred, LEASE_WRITE);
1403 error = VOP_SYMLINK(nd.ni_dvp, &nd.ni_vp, &nd.ni_cnd, &vattr, path);
1404 NDFREE(&nd, NDF_ONLY_PNBUF);
1408 ASSERT_VOP_UNLOCKED(nd.ni_dvp, "symlink");
1409 ASSERT_VOP_UNLOCKED(nd.ni_vp, "symlink");
1411 zfree(namei_zone, path);
1416 * Delete a whiteout from the filesystem.
1422 register struct undelete_args /* {
1423 syscallarg(char *) path;
1427 struct nameidata nd;
1430 NDINIT(&nd, DELETE, LOCKPARENT|DOWHITEOUT, UIO_USERSPACE,
1431 SCARG(uap, path), p);
1436 if (nd.ni_vp != NULLVP || !(nd.ni_cnd.cn_flags & ISWHITEOUT)) {
1437 NDFREE(&nd, NDF_ONLY_PNBUF);
1438 if (nd.ni_dvp == nd.ni_vp)
1447 VOP_LEASE(nd.ni_dvp, p, p->p_ucred, LEASE_WRITE);
1448 error = VOP_WHITEOUT(nd.ni_dvp, &nd.ni_cnd, DELETE);
1449 NDFREE(&nd, NDF_ONLY_PNBUF);
1451 ASSERT_VOP_UNLOCKED(nd.ni_dvp, "undelete");
1452 ASSERT_VOP_UNLOCKED(nd.ni_vp, "undelete");
1457 * Delete a name from the filesystem.
1459 #ifndef _SYS_SYSPROTO_H_
1460 struct unlink_args {
1468 struct unlink_args /* {
1469 syscallarg(char *) path;
1472 register struct vnode *vp;
1474 struct nameidata nd;
1477 NDINIT(&nd, DELETE, LOCKPARENT, UIO_USERSPACE, SCARG(uap, path), p);
1478 if ((error = namei(&nd)) != 0)
1481 VOP_LEASE(vp, p, p->p_ucred, LEASE_WRITE);
1482 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY, p);
1484 if (vp->v_type == VDIR)
1485 error = EPERM; /* POSIX */
1488 * The root of a mounted filesystem cannot be deleted.
1490 * XXX: can this only be a VDIR case?
1492 if (vp->v_flag & VROOT)
1497 VOP_LEASE(nd.ni_dvp, p, p->p_ucred, LEASE_WRITE);
1498 error = VOP_REMOVE(nd.ni_dvp, vp, &nd.ni_cnd);
1500 NDFREE(&nd, NDF_ONLY_PNBUF);
1501 if (nd.ni_dvp == vp)
1507 ASSERT_VOP_UNLOCKED(nd.ni_dvp, "unlink");
1508 ASSERT_VOP_UNLOCKED(nd.ni_vp, "unlink");
1513 * Reposition read/write file offset.
1515 #ifndef _SYS_SYSPROTO_H_
1526 register struct lseek_args /* {
1528 syscallarg(int) pad;
1529 syscallarg(off_t) offset;
1530 syscallarg(int) whence;
1533 struct ucred *cred = p->p_ucred;
1534 register struct filedesc *fdp = p->p_fd;
1535 register struct file *fp;
1539 if ((u_int)SCARG(uap, fd) >= fdp->fd_nfiles ||
1540 (fp = fdp->fd_ofiles[SCARG(uap, fd)]) == NULL)
1542 if (fp->f_type != DTYPE_VNODE)
1544 switch (SCARG(uap, whence)) {
1546 fp->f_offset += SCARG(uap, offset);
1549 error=VOP_GETATTR((struct vnode *)fp->f_data, &vattr, cred, p);
1552 fp->f_offset = SCARG(uap, offset) + vattr.va_size;
1555 fp->f_offset = SCARG(uap, offset);
1560 *(off_t *)(p->p_retval) = fp->f_offset;
1564 #if defined(COMPAT_43) || defined(COMPAT_SUNOS)
1566 * Reposition read/write file offset.
1568 #ifndef _SYS_SYSPROTO_H_
1569 struct olseek_args {
1578 register struct olseek_args /* {
1580 syscallarg(long) offset;
1581 syscallarg(int) whence;
1584 struct lseek_args /* {
1586 syscallarg(int) pad;
1587 syscallarg(off_t) offset;
1588 syscallarg(int) whence;
1592 SCARG(&nuap, fd) = SCARG(uap, fd);
1593 SCARG(&nuap, offset) = SCARG(uap, offset);
1594 SCARG(&nuap, whence) = SCARG(uap, whence);
1595 error = lseek(p, &nuap);
1598 #endif /* COMPAT_43 */
1601 * Check access permissions.
1603 #ifndef _SYS_SYSPROTO_H_
1604 struct access_args {
1612 register struct access_args /* {
1613 syscallarg(char *) path;
1614 syscallarg(int) flags;
1617 struct ucred *cred, *tmpcred;
1618 register struct vnode *vp;
1620 struct nameidata nd;
1624 * Create and modify a temporary credential instead of one that
1625 * is potentially shared. This could also mess up socket
1626 * buffer accounting which can run in an interrupt context.
1628 tmpcred = crdup(cred);
1629 tmpcred->cr_uid = p->p_cred->p_ruid;
1630 tmpcred->cr_groups[0] = p->p_cred->p_rgid;
1631 p->p_ucred = tmpcred;
1632 NDINIT(&nd, LOOKUP, FOLLOW | LOCKLEAF | NOOBJ, UIO_USERSPACE,
1633 SCARG(uap, path), p);
1634 if ((error = namei(&nd)) != 0)
1638 /* Flags == 0 means only check for existence. */
1639 if (SCARG(uap, flags)) {
1641 if (SCARG(uap, flags) & R_OK)
1643 if (SCARG(uap, flags) & W_OK)
1645 if (SCARG(uap, flags) & X_OK)
1647 if ((flags & VWRITE) == 0 || (error = vn_writechk(vp)) == 0)
1648 error = VOP_ACCESS(vp, flags, tmpcred, p);
1650 NDFREE(&nd, NDF_ONLY_PNBUF);
1658 #if defined(COMPAT_43) || defined(COMPAT_SUNOS)
1660 * Get file status; this version follows links.
1662 #ifndef _SYS_SYSPROTO_H_
1672 register struct ostat_args /* {
1673 syscallarg(char *) path;
1674 syscallarg(struct ostat *) ub;
1680 struct nameidata nd;
1682 NDINIT(&nd, LOOKUP, FOLLOW | LOCKLEAF | NOOBJ, UIO_USERSPACE,
1683 SCARG(uap, path), p);
1684 if ((error = namei(&nd)) != 0)
1686 NDFREE(&nd, NDF_ONLY_PNBUF);
1687 error = vn_stat(nd.ni_vp, &sb, p);
1692 error = copyout((caddr_t)&osb, (caddr_t)SCARG(uap, ub), sizeof (osb));
1697 * Get file status; this version does not follow links.
1699 #ifndef _SYS_SYSPROTO_H_
1700 struct olstat_args {
1709 register struct olstat_args /* {
1710 syscallarg(char *) path;
1711 syscallarg(struct ostat *) ub;
1718 struct nameidata nd;
1720 NDINIT(&nd, LOOKUP, NOFOLLOW | LOCKLEAF | NOOBJ, UIO_USERSPACE,
1721 SCARG(uap, path), p);
1722 if ((error = namei(&nd)) != 0)
1725 error = vn_stat(vp, &sb, p);
1726 NDFREE(&nd, NDF_ONLY_PNBUF);
1731 error = copyout((caddr_t)&osb, (caddr_t)SCARG(uap, ub), sizeof (osb));
1736 * Convert from an old to a new stat structure.
1744 ost->st_dev = st->st_dev;
1745 ost->st_ino = st->st_ino;
1746 ost->st_mode = st->st_mode;
1747 ost->st_nlink = st->st_nlink;
1748 ost->st_uid = st->st_uid;
1749 ost->st_gid = st->st_gid;
1750 ost->st_rdev = st->st_rdev;
1751 if (st->st_size < (quad_t)1 << 32)
1752 ost->st_size = st->st_size;
1755 ost->st_atime = st->st_atime;
1756 ost->st_mtime = st->st_mtime;
1757 ost->st_ctime = st->st_ctime;
1758 ost->st_blksize = st->st_blksize;
1759 ost->st_blocks = st->st_blocks;
1760 ost->st_flags = st->st_flags;
1761 ost->st_gen = st->st_gen;
1763 #endif /* COMPAT_43 || COMPAT_SUNOS */
1766 * Get file status; this version follows links.
1768 #ifndef _SYS_SYSPROTO_H_
1778 register struct stat_args /* {
1779 syscallarg(char *) path;
1780 syscallarg(struct stat *) ub;
1785 struct nameidata nd;
1787 NDINIT(&nd, LOOKUP, FOLLOW | LOCKLEAF | NOOBJ, UIO_USERSPACE,
1788 SCARG(uap, path), p);
1789 if ((error = namei(&nd)) != 0)
1791 error = vn_stat(nd.ni_vp, &sb, p);
1792 NDFREE(&nd, NDF_ONLY_PNBUF);
1796 error = copyout((caddr_t)&sb, (caddr_t)SCARG(uap, ub), sizeof (sb));
1801 * Get file status; this version does not follow links.
1803 #ifndef _SYS_SYSPROTO_H_
1813 register struct lstat_args /* {
1814 syscallarg(char *) path;
1815 syscallarg(struct stat *) ub;
1821 struct nameidata nd;
1823 NDINIT(&nd, LOOKUP, NOFOLLOW | LOCKLEAF | NOOBJ, UIO_USERSPACE,
1824 SCARG(uap, path), p);
1825 if ((error = namei(&nd)) != 0)
1828 error = vn_stat(vp, &sb, p);
1829 NDFREE(&nd, NDF_ONLY_PNBUF);
1833 error = copyout((caddr_t)&sb, (caddr_t)SCARG(uap, ub), sizeof (sb));
1842 nsb->st_dev = sb->st_dev;
1843 nsb->st_ino = sb->st_ino;
1844 nsb->st_mode = sb->st_mode;
1845 nsb->st_nlink = sb->st_nlink;
1846 nsb->st_uid = sb->st_uid;
1847 nsb->st_gid = sb->st_gid;
1848 nsb->st_rdev = sb->st_rdev;
1849 nsb->st_atimespec = sb->st_atimespec;
1850 nsb->st_mtimespec = sb->st_mtimespec;
1851 nsb->st_ctimespec = sb->st_ctimespec;
1852 nsb->st_size = sb->st_size;
1853 nsb->st_blocks = sb->st_blocks;
1854 nsb->st_blksize = sb->st_blksize;
1855 nsb->st_flags = sb->st_flags;
1856 nsb->st_gen = sb->st_gen;
1857 nsb->st_qspare[0] = sb->st_qspare[0];
1858 nsb->st_qspare[1] = sb->st_qspare[1];
1861 #ifndef _SYS_SYSPROTO_H_
1871 register struct nstat_args /* {
1872 syscallarg(char *) path;
1873 syscallarg(struct nstat *) ub;
1879 struct nameidata nd;
1881 NDINIT(&nd, LOOKUP, FOLLOW | LOCKLEAF | NOOBJ, UIO_USERSPACE,
1882 SCARG(uap, path), p);
1883 if ((error = namei(&nd)) != 0)
1885 NDFREE(&nd, NDF_ONLY_PNBUF);
1886 error = vn_stat(nd.ni_vp, &sb, p);
1890 cvtnstat(&sb, &nsb);
1891 error = copyout((caddr_t)&nsb, (caddr_t)SCARG(uap, ub), sizeof (nsb));
1896 * Get file status; this version does not follow links.
1898 #ifndef _SYS_SYSPROTO_H_
1908 register struct nlstat_args /* {
1909 syscallarg(char *) path;
1910 syscallarg(struct nstat *) ub;
1917 struct nameidata nd;
1919 NDINIT(&nd, LOOKUP, NOFOLLOW | LOCKLEAF | NOOBJ, UIO_USERSPACE,
1920 SCARG(uap, path), p);
1921 if ((error = namei(&nd)) != 0)
1924 NDFREE(&nd, NDF_ONLY_PNBUF);
1925 error = vn_stat(vp, &sb, p);
1929 cvtnstat(&sb, &nsb);
1930 error = copyout((caddr_t)&nsb, (caddr_t)SCARG(uap, ub), sizeof (nsb));
1935 * Get configurable pathname variables.
1937 #ifndef _SYS_SYSPROTO_H_
1938 struct pathconf_args {
1947 register struct pathconf_args /* {
1948 syscallarg(char *) path;
1949 syscallarg(int) name;
1953 struct nameidata nd;
1955 NDINIT(&nd, LOOKUP, FOLLOW | LOCKLEAF | NOOBJ, UIO_USERSPACE,
1956 SCARG(uap, path), p);
1957 if ((error = namei(&nd)) != 0)
1959 NDFREE(&nd, NDF_ONLY_PNBUF);
1960 error = VOP_PATHCONF(nd.ni_vp, SCARG(uap, name), p->p_retval);
1966 * Return target name of a symbolic link.
1968 #ifndef _SYS_SYSPROTO_H_
1969 struct readlink_args {
1979 register struct readlink_args /* {
1980 syscallarg(char *) path;
1981 syscallarg(char *) buf;
1982 syscallarg(int) count;
1985 register struct vnode *vp;
1989 struct nameidata nd;
1991 NDINIT(&nd, LOOKUP, NOFOLLOW | LOCKLEAF | NOOBJ, UIO_USERSPACE,
1992 SCARG(uap, path), p);
1993 if ((error = namei(&nd)) != 0)
1995 NDFREE(&nd, NDF_ONLY_PNBUF);
1997 if (vp->v_type != VLNK)
2000 aiov.iov_base = SCARG(uap, buf);
2001 aiov.iov_len = SCARG(uap, count);
2002 auio.uio_iov = &aiov;
2003 auio.uio_iovcnt = 1;
2004 auio.uio_offset = 0;
2005 auio.uio_rw = UIO_READ;
2006 auio.uio_segflg = UIO_USERSPACE;
2008 auio.uio_resid = SCARG(uap, count);
2009 error = VOP_READLINK(vp, &auio, p->p_ucred);
2012 p->p_retval[0] = SCARG(uap, count) - auio.uio_resid;
2017 setfflags(p, vp, flags)
2026 * Prevent non-root users from setting flags on devices. When
2027 * a device is reused, users can retain ownership of the device
2028 * if they are allowed to set flags and programs assume that
2029 * chown can't fail when done as root.
2031 if ((vp->v_type == VCHR || vp->v_type == VBLK) &&
2032 ((error = suser_xxx(p->p_ucred, p, PRISON_ROOT)) != 0))
2035 VOP_LEASE(vp, p, p->p_ucred, LEASE_WRITE);
2036 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY, p);
2038 vattr.va_flags = flags;
2039 error = VOP_SETATTR(vp, &vattr, p->p_ucred, p);
2040 VOP_UNLOCK(vp, 0, p);
2045 * Change flags of a file given a path name.
2047 #ifndef _SYS_SYSPROTO_H_
2048 struct chflags_args {
2057 register struct chflags_args /* {
2058 syscallarg(char *) path;
2059 syscallarg(int) flags;
2063 struct nameidata nd;
2065 NDINIT(&nd, LOOKUP, FOLLOW, UIO_USERSPACE, SCARG(uap, path), p);
2066 if ((error = namei(&nd)) != 0)
2068 NDFREE(&nd, NDF_ONLY_PNBUF);
2069 error = setfflags(p, nd.ni_vp, SCARG(uap, flags));
2075 * Change flags of a file given a file descriptor.
2077 #ifndef _SYS_SYSPROTO_H_
2078 struct fchflags_args {
2087 register struct fchflags_args /* {
2089 syscallarg(int) flags;
2095 if ((error = getvnode(p->p_fd, SCARG(uap, fd), &fp)) != 0)
2097 return setfflags(p, (struct vnode *) fp->f_data, SCARG(uap, flags));
2101 setfmode(p, vp, mode)
2109 VOP_LEASE(vp, p, p->p_ucred, LEASE_WRITE);
2110 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY, p);
2112 vattr.va_mode = mode & ALLPERMS;
2113 error = VOP_SETATTR(vp, &vattr, p->p_ucred, p);
2114 VOP_UNLOCK(vp, 0, p);
2119 * Change mode of a file given path name.
2121 #ifndef _SYS_SYSPROTO_H_
2131 register struct chmod_args /* {
2132 syscallarg(char *) path;
2133 syscallarg(int) mode;
2137 struct nameidata nd;
2139 NDINIT(&nd, LOOKUP, FOLLOW, UIO_USERSPACE, SCARG(uap, path), p);
2140 if ((error = namei(&nd)) != 0)
2142 NDFREE(&nd, NDF_ONLY_PNBUF);
2143 error = setfmode(p, nd.ni_vp, SCARG(uap, mode));
2149 * Change mode of a file given path name (don't follow links.)
2151 #ifndef _SYS_SYSPROTO_H_
2152 struct lchmod_args {
2161 register struct lchmod_args /* {
2162 syscallarg(char *) path;
2163 syscallarg(int) mode;
2167 struct nameidata nd;
2169 NDINIT(&nd, LOOKUP, NOFOLLOW, UIO_USERSPACE, SCARG(uap, path), p);
2170 if ((error = namei(&nd)) != 0)
2172 NDFREE(&nd, NDF_ONLY_PNBUF);
2173 error = setfmode(p, nd.ni_vp, SCARG(uap, mode));
2179 * Change mode of a file given a file descriptor.
2181 #ifndef _SYS_SYSPROTO_H_
2182 struct fchmod_args {
2191 register struct fchmod_args /* {
2193 syscallarg(int) mode;
2199 if ((error = getvnode(p->p_fd, SCARG(uap, fd), &fp)) != 0)
2201 return setfmode(p, (struct vnode *)fp->f_data, SCARG(uap, mode));
2205 setfown(p, vp, uid, gid)
2214 VOP_LEASE(vp, p, p->p_ucred, LEASE_WRITE);
2215 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY, p);
2219 error = VOP_SETATTR(vp, &vattr, p->p_ucred, p);
2220 VOP_UNLOCK(vp, 0, p);
2225 * Set ownership given a path name.
2227 #ifndef _SYS_SYSPROTO_H_
2238 register struct chown_args /* {
2239 syscallarg(char *) path;
2240 syscallarg(int) uid;
2241 syscallarg(int) gid;
2245 struct nameidata nd;
2247 NDINIT(&nd, LOOKUP, FOLLOW, UIO_USERSPACE, SCARG(uap, path), p);
2248 if ((error = namei(&nd)) != 0)
2250 NDFREE(&nd, NDF_ONLY_PNBUF);
2251 error = setfown(p, nd.ni_vp, SCARG(uap, uid), SCARG(uap, gid));
2257 * Set ownership given a path name, do not cross symlinks.
2259 #ifndef _SYS_SYSPROTO_H_
2260 struct lchown_args {
2270 register struct lchown_args /* {
2271 syscallarg(char *) path;
2272 syscallarg(int) uid;
2273 syscallarg(int) gid;
2277 struct nameidata nd;
2279 NDINIT(&nd, LOOKUP, NOFOLLOW, UIO_USERSPACE, SCARG(uap, path), p);
2280 if ((error = namei(&nd)) != 0)
2282 NDFREE(&nd, NDF_ONLY_PNBUF);
2283 error = setfown(p, nd.ni_vp, SCARG(uap, uid), SCARG(uap, gid));
2289 * Set ownership given a file descriptor.
2291 #ifndef _SYS_SYSPROTO_H_
2292 struct fchown_args {
2302 register struct fchown_args /* {
2304 syscallarg(int) uid;
2305 syscallarg(int) gid;
2311 if ((error = getvnode(p->p_fd, SCARG(uap, fd), &fp)) != 0)
2313 return setfown(p, (struct vnode *)fp->f_data,
2314 SCARG(uap, uid), SCARG(uap, gid));
2318 getutimes(usrtvp, tsp)
2319 const struct timeval *usrtvp;
2320 struct timespec *tsp;
2322 struct timeval tv[2];
2325 if (usrtvp == NULL) {
2327 TIMEVAL_TO_TIMESPEC(&tv[0], &tsp[0]);
2330 if ((error = copyin(usrtvp, tv, sizeof (tv))) != 0)
2332 TIMEVAL_TO_TIMESPEC(&tv[0], &tsp[0]);
2333 TIMEVAL_TO_TIMESPEC(&tv[1], &tsp[1]);
2339 setutimes(p, vp, ts, nullflag)
2342 const struct timespec *ts;
2348 VOP_LEASE(vp, p, p->p_ucred, LEASE_WRITE);
2349 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY, p);
2351 vattr.va_atime = ts[0];
2352 vattr.va_mtime = ts[1];
2354 vattr.va_vaflags |= VA_UTIMES_NULL;
2355 error = VOP_SETATTR(vp, &vattr, p->p_ucred, p);
2356 VOP_UNLOCK(vp, 0, p);
2361 * Set the access and modification times of a file.
2363 #ifndef _SYS_SYSPROTO_H_
2364 struct utimes_args {
2366 struct timeval *tptr;
2373 register struct utimes_args /* {
2374 syscallarg(char *) path;
2375 syscallarg(struct timeval *) tptr;
2378 struct timespec ts[2];
2379 struct timeval *usrtvp;
2381 struct nameidata nd;
2383 usrtvp = SCARG(uap, tptr);
2384 if ((error = getutimes(usrtvp, ts)) != 0)
2386 NDINIT(&nd, LOOKUP, FOLLOW, UIO_USERSPACE, SCARG(uap, path), p);
2387 if ((error = namei(&nd)) != 0)
2389 NDFREE(&nd, NDF_ONLY_PNBUF);
2390 error = setutimes(p, nd.ni_vp, ts, usrtvp == NULL);
2396 * Set the access and modification times of a file.
2398 #ifndef _SYS_SYSPROTO_H_
2399 struct lutimes_args {
2401 struct timeval *tptr;
2408 register struct lutimes_args /* {
2409 syscallarg(char *) path;
2410 syscallarg(struct timeval *) tptr;
2413 struct timespec ts[2];
2414 struct timeval *usrtvp;
2416 struct nameidata nd;
2418 usrtvp = SCARG(uap, tptr);
2419 if ((error = getutimes(usrtvp, ts)) != 0)
2421 NDINIT(&nd, LOOKUP, NOFOLLOW, UIO_USERSPACE, SCARG(uap, path), p);
2422 if ((error = namei(&nd)) != 0)
2424 NDFREE(&nd, NDF_ONLY_PNBUF);
2425 error = setutimes(p, nd.ni_vp, ts, usrtvp == NULL);
2431 * Set the access and modification times of a file.
2433 #ifndef _SYS_SYSPROTO_H_
2434 struct futimes_args {
2436 struct timeval *tptr;
2443 register struct futimes_args /* {
2444 syscallarg(int ) fd;
2445 syscallarg(struct timeval *) tptr;
2448 struct timespec ts[2];
2450 struct timeval *usrtvp;
2453 usrtvp = SCARG(uap, tptr);
2454 if ((error = getutimes(usrtvp, ts)) != 0)
2456 if ((error = getvnode(p->p_fd, SCARG(uap, fd), &fp)) != 0)
2458 return setutimes(p, (struct vnode *)fp->f_data, ts, usrtvp == NULL);
2462 * Truncate a file given its path name.
2464 #ifndef _SYS_SYSPROTO_H_
2465 struct truncate_args {
2475 register struct truncate_args /* {
2476 syscallarg(char *) path;
2477 syscallarg(int) pad;
2478 syscallarg(off_t) length;
2481 register struct vnode *vp;
2484 struct nameidata nd;
2486 if (uap->length < 0)
2488 NDINIT(&nd, LOOKUP, FOLLOW, UIO_USERSPACE, SCARG(uap, path), p);
2489 if ((error = namei(&nd)) != 0)
2492 NDFREE(&nd, NDF_ONLY_PNBUF);
2493 VOP_LEASE(vp, p, p->p_ucred, LEASE_WRITE);
2494 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY, p);
2495 if (vp->v_type == VDIR)
2497 else if ((error = vn_writechk(vp)) == 0 &&
2498 (error = VOP_ACCESS(vp, VWRITE, p->p_ucred, p)) == 0) {
2500 vattr.va_size = SCARG(uap, length);
2501 error = VOP_SETATTR(vp, &vattr, p->p_ucred, p);
2508 * Truncate a file given a file descriptor.
2510 #ifndef _SYS_SYSPROTO_H_
2511 struct ftruncate_args {
2521 register struct ftruncate_args /* {
2523 syscallarg(int) pad;
2524 syscallarg(off_t) length;
2532 if (uap->length < 0)
2534 if ((error = getvnode(p->p_fd, SCARG(uap, fd), &fp)) != 0)
2536 if ((fp->f_flag & FWRITE) == 0)
2538 vp = (struct vnode *)fp->f_data;
2539 VOP_LEASE(vp, p, p->p_ucred, LEASE_WRITE);
2540 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY, p);
2541 if (vp->v_type == VDIR)
2543 else if ((error = vn_writechk(vp)) == 0) {
2545 vattr.va_size = SCARG(uap, length);
2546 error = VOP_SETATTR(vp, &vattr, fp->f_cred, p);
2548 VOP_UNLOCK(vp, 0, p);
2552 #if defined(COMPAT_43) || defined(COMPAT_SUNOS)
2554 * Truncate a file given its path name.
2556 #ifndef _SYS_SYSPROTO_H_
2557 struct otruncate_args {
2566 register struct otruncate_args /* {
2567 syscallarg(char *) path;
2568 syscallarg(long) length;
2571 struct truncate_args /* {
2572 syscallarg(char *) path;
2573 syscallarg(int) pad;
2574 syscallarg(off_t) length;
2577 SCARG(&nuap, path) = SCARG(uap, path);
2578 SCARG(&nuap, length) = SCARG(uap, length);
2579 return (truncate(p, &nuap));
2583 * Truncate a file given a file descriptor.
2585 #ifndef _SYS_SYSPROTO_H_
2586 struct oftruncate_args {
2595 register struct oftruncate_args /* {
2597 syscallarg(long) length;
2600 struct ftruncate_args /* {
2602 syscallarg(int) pad;
2603 syscallarg(off_t) length;
2606 SCARG(&nuap, fd) = SCARG(uap, fd);
2607 SCARG(&nuap, length) = SCARG(uap, length);
2608 return (ftruncate(p, &nuap));
2610 #endif /* COMPAT_43 || COMPAT_SUNOS */
2613 * Sync an open file.
2615 #ifndef _SYS_SYSPROTO_H_
2624 struct fsync_args /* {
2628 register struct vnode *vp;
2633 if ((error = getvnode(p->p_fd, SCARG(uap, fd), &fp)) != 0)
2635 vp = (struct vnode *)fp->f_data;
2636 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY, p);
2637 if (VOP_GETVOBJECT(vp, &obj) == 0)
2638 vm_object_page_clean(obj, 0, 0, 0);
2639 if ((error = VOP_FSYNC(vp, fp->f_cred, MNT_WAIT, p)) == 0 &&
2640 vp->v_mount && (vp->v_mount->mnt_flag & MNT_SOFTDEP) &&
2642 error = (*bioops.io_fsync)(vp);
2643 VOP_UNLOCK(vp, 0, p);
2648 * Rename files. Source and destination must either both be directories,
2649 * or both not be directories. If target is a directory, it must be empty.
2651 #ifndef _SYS_SYSPROTO_H_
2652 struct rename_args {
2661 register struct rename_args /* {
2662 syscallarg(char *) from;
2663 syscallarg(char *) to;
2666 register struct vnode *tvp, *fvp, *tdvp;
2667 struct nameidata fromnd, tond;
2671 NDINIT(&fromnd, DELETE, WANTPARENT | SAVESTART, UIO_USERSPACE,
2672 SCARG(uap, from), p);
2673 if ((error = namei(&fromnd)) != 0)
2676 NDINIT(&tond, RENAME, LOCKPARENT | LOCKLEAF | NOCACHE | SAVESTART | NOOBJ,
2677 UIO_USERSPACE, SCARG(uap, to), p);
2678 if (fromnd.ni_vp->v_type == VDIR)
2679 tond.ni_cnd.cn_flags |= WILLBEDIR;
2680 if ((error = namei(&tond)) != 0) {
2681 /* Translate error code for rename("dir1", "dir2/."). */
2682 if (error == EISDIR && fvp->v_type == VDIR)
2684 NDFREE(&fromnd, NDF_ONLY_PNBUF);
2685 vrele(fromnd.ni_dvp);
2692 if (fvp->v_type == VDIR && tvp->v_type != VDIR) {
2695 } else if (fvp->v_type != VDIR && tvp->v_type == VDIR) {
2703 * If the source is the same as the destination (that is, if they
2704 * are links to the same vnode), then there is nothing to do.
2710 VOP_LEASE(tdvp, p, p->p_ucred, LEASE_WRITE);
2711 if (fromnd.ni_dvp != tdvp) {
2712 VOP_LEASE(fromnd.ni_dvp, p, p->p_ucred, LEASE_WRITE);
2715 VOP_LEASE(tvp, p, p->p_ucred, LEASE_WRITE);
2717 error = VOP_RENAME(fromnd.ni_dvp, fromnd.ni_vp, &fromnd.ni_cnd,
2718 tond.ni_dvp, tond.ni_vp, &tond.ni_cnd);
2719 NDFREE(&fromnd, NDF_ONLY_PNBUF);
2720 NDFREE(&tond, NDF_ONLY_PNBUF);
2722 NDFREE(&fromnd, NDF_ONLY_PNBUF);
2723 NDFREE(&tond, NDF_ONLY_PNBUF);
2730 vrele(fromnd.ni_dvp);
2733 vrele(tond.ni_startdir);
2734 ASSERT_VOP_UNLOCKED(fromnd.ni_dvp, "rename");
2735 ASSERT_VOP_UNLOCKED(fromnd.ni_vp, "rename");
2736 ASSERT_VOP_UNLOCKED(tond.ni_dvp, "rename");
2737 ASSERT_VOP_UNLOCKED(tond.ni_vp, "rename");
2739 if (fromnd.ni_startdir)
2740 vrele(fromnd.ni_startdir);
2747 * Make a directory file.
2749 #ifndef _SYS_SYSPROTO_H_
2759 register struct mkdir_args /* {
2760 syscallarg(char *) path;
2761 syscallarg(int) mode;
2764 register struct vnode *vp;
2767 struct nameidata nd;
2770 NDINIT(&nd, CREATE, LOCKPARENT, UIO_USERSPACE, SCARG(uap, path), p);
2771 nd.ni_cnd.cn_flags |= WILLBEDIR;
2772 if ((error = namei(&nd)) != 0)
2776 NDFREE(&nd, NDF_ONLY_PNBUF);
2777 if (nd.ni_dvp == vp)
2785 vattr.va_type = VDIR;
2786 vattr.va_mode = (SCARG(uap, mode) & ACCESSPERMS) &~ p->p_fd->fd_cmask;
2787 VOP_LEASE(nd.ni_dvp, p, p->p_ucred, LEASE_WRITE);
2788 error = VOP_MKDIR(nd.ni_dvp, &nd.ni_vp, &nd.ni_cnd, &vattr);
2789 NDFREE(&nd, NDF_ONLY_PNBUF);
2793 ASSERT_VOP_UNLOCKED(nd.ni_dvp, "mkdir");
2794 ASSERT_VOP_UNLOCKED(nd.ni_vp, "mkdir");
2799 * Remove a directory file.
2801 #ifndef _SYS_SYSPROTO_H_
2810 struct rmdir_args /* {
2811 syscallarg(char *) path;
2814 register struct vnode *vp;
2816 struct nameidata nd;
2819 NDINIT(&nd, DELETE, LOCKPARENT | LOCKLEAF, UIO_USERSPACE,
2820 SCARG(uap, path), p);
2821 if ((error = namei(&nd)) != 0)
2824 if (vp->v_type != VDIR) {
2829 * No rmdir "." please.
2831 if (nd.ni_dvp == vp) {
2836 * The root of a mounted filesystem cannot be deleted.
2838 if (vp->v_flag & VROOT)
2841 VOP_LEASE(nd.ni_dvp, p, p->p_ucred, LEASE_WRITE);
2842 VOP_LEASE(vp, p, p->p_ucred, LEASE_WRITE);
2843 error = VOP_RMDIR(nd.ni_dvp, nd.ni_vp, &nd.ni_cnd);
2846 NDFREE(&nd, NDF_ONLY_PNBUF);
2847 if (nd.ni_dvp == vp)
2853 ASSERT_VOP_UNLOCKED(nd.ni_dvp, "rmdir");
2854 ASSERT_VOP_UNLOCKED(nd.ni_vp, "rmdir");
2860 * Read a block of directory entries in a file system independent format.
2862 #ifndef _SYS_SYSPROTO_H_
2863 struct ogetdirentries_args {
2871 ogetdirentries(p, uap)
2873 register struct ogetdirentries_args /* {
2875 syscallarg(char *) buf;
2876 syscallarg(u_int) count;
2877 syscallarg(long *) basep;
2882 struct uio auio, kuio;
2883 struct iovec aiov, kiov;
2884 struct dirent *dp, *edp;
2886 int error, eofflag, readcnt;
2889 /* XXX arbitrary sanity limit on `count'. */
2890 if (SCARG(uap, count) > 64 * 1024)
2892 if ((error = getvnode(p->p_fd, SCARG(uap, fd), &fp)) != 0)
2894 if ((fp->f_flag & FREAD) == 0)
2896 vp = (struct vnode *)fp->f_data;
2898 if (vp->v_type != VDIR)
2900 aiov.iov_base = SCARG(uap, buf);
2901 aiov.iov_len = SCARG(uap, count);
2902 auio.uio_iov = &aiov;
2903 auio.uio_iovcnt = 1;
2904 auio.uio_rw = UIO_READ;
2905 auio.uio_segflg = UIO_USERSPACE;
2907 auio.uio_resid = SCARG(uap, count);
2908 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY, p);
2909 loff = auio.uio_offset = fp->f_offset;
2910 # if (BYTE_ORDER != LITTLE_ENDIAN)
2911 if (vp->v_mount->mnt_maxsymlinklen <= 0) {
2912 error = VOP_READDIR(vp, &auio, fp->f_cred, &eofflag,
2914 fp->f_offset = auio.uio_offset;
2919 kuio.uio_iov = &kiov;
2920 kuio.uio_segflg = UIO_SYSSPACE;
2921 kiov.iov_len = SCARG(uap, count);
2922 MALLOC(dirbuf, caddr_t, SCARG(uap, count), M_TEMP, M_WAITOK);
2923 kiov.iov_base = dirbuf;
2924 error = VOP_READDIR(vp, &kuio, fp->f_cred, &eofflag,
2926 fp->f_offset = kuio.uio_offset;
2928 readcnt = SCARG(uap, count) - kuio.uio_resid;
2929 edp = (struct dirent *)&dirbuf[readcnt];
2930 for (dp = (struct dirent *)dirbuf; dp < edp; ) {
2931 # if (BYTE_ORDER == LITTLE_ENDIAN)
2933 * The expected low byte of
2934 * dp->d_namlen is our dp->d_type.
2935 * The high MBZ byte of dp->d_namlen
2936 * is our dp->d_namlen.
2938 dp->d_type = dp->d_namlen;
2942 * The dp->d_type is the high byte
2943 * of the expected dp->d_namlen,
2944 * so must be zero'ed.
2948 if (dp->d_reclen > 0) {
2949 dp = (struct dirent *)
2950 ((char *)dp + dp->d_reclen);
2957 error = uiomove(dirbuf, readcnt, &auio);
2959 FREE(dirbuf, M_TEMP);
2961 VOP_UNLOCK(vp, 0, p);
2964 if (SCARG(uap, count) == auio.uio_resid) {
2965 if (union_dircheckp) {
2966 error = union_dircheckp(p, &vp, fp);
2972 if ((vp->v_flag & VROOT) &&
2973 (vp->v_mount->mnt_flag & MNT_UNION)) {
2974 struct vnode *tvp = vp;
2975 vp = vp->v_mount->mnt_vnodecovered;
2977 fp->f_data = (caddr_t) vp;
2983 error = copyout((caddr_t)&loff, (caddr_t)SCARG(uap, basep),
2985 p->p_retval[0] = SCARG(uap, count) - auio.uio_resid;
2988 #endif /* COMPAT_43 */
2991 * Read a block of directory entries in a file system independent format.
2993 #ifndef _SYS_SYSPROTO_H_
2994 struct getdirentries_args {
3002 getdirentries(p, uap)
3004 register struct getdirentries_args /* {
3006 syscallarg(char *) buf;
3007 syscallarg(u_int) count;
3008 syscallarg(long *) basep;
3018 if ((error = getvnode(p->p_fd, SCARG(uap, fd), &fp)) != 0)
3020 if ((fp->f_flag & FREAD) == 0)
3022 vp = (struct vnode *)fp->f_data;
3024 if (vp->v_type != VDIR)
3026 aiov.iov_base = SCARG(uap, buf);
3027 aiov.iov_len = SCARG(uap, count);
3028 auio.uio_iov = &aiov;
3029 auio.uio_iovcnt = 1;
3030 auio.uio_rw = UIO_READ;
3031 auio.uio_segflg = UIO_USERSPACE;
3033 auio.uio_resid = SCARG(uap, count);
3034 /* vn_lock(vp, LK_SHARED | LK_RETRY, p); */
3035 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY, p);
3036 loff = auio.uio_offset = fp->f_offset;
3037 error = VOP_READDIR(vp, &auio, fp->f_cred, &eofflag, NULL, NULL);
3038 fp->f_offset = auio.uio_offset;
3039 VOP_UNLOCK(vp, 0, p);
3042 if (SCARG(uap, count) == auio.uio_resid) {
3043 if (union_dircheckp) {
3044 error = union_dircheckp(p, &vp, fp);
3050 if ((vp->v_flag & VROOT) &&
3051 (vp->v_mount->mnt_flag & MNT_UNION)) {
3052 struct vnode *tvp = vp;
3053 vp = vp->v_mount->mnt_vnodecovered;
3055 fp->f_data = (caddr_t) vp;
3061 if (SCARG(uap, basep) != NULL) {
3062 error = copyout((caddr_t)&loff, (caddr_t)SCARG(uap, basep),
3065 p->p_retval[0] = SCARG(uap, count) - auio.uio_resid;
3068 #ifndef _SYS_SYSPROTO_H_
3069 struct getdents_args {
3078 register struct getdents_args /* {
3080 syscallarg(char *) buf;
3081 syscallarg(u_int) count;
3084 struct getdirentries_args ap;
3087 ap.count = uap->count;
3089 return getdirentries(p, &ap);
3093 * Set the mode mask for creation of filesystem nodes.
3097 #ifndef _SYS_SYSPROTO_H_
3105 struct umask_args /* {
3106 syscallarg(int) newmask;
3109 register struct filedesc *fdp;
3112 p->p_retval[0] = fdp->fd_cmask;
3113 fdp->fd_cmask = SCARG(uap, newmask) & ALLPERMS;
3118 * Void all references to file by ripping underlying filesystem
3121 #ifndef _SYS_SYSPROTO_H_
3122 struct revoke_args {
3130 register struct revoke_args /* {
3131 syscallarg(char *) path;
3134 register struct vnode *vp;
3137 struct nameidata nd;
3139 NDINIT(&nd, LOOKUP, FOLLOW, UIO_USERSPACE, SCARG(uap, path), p);
3140 if ((error = namei(&nd)) != 0)
3143 NDFREE(&nd, NDF_ONLY_PNBUF);
3144 if (vp->v_type != VCHR && vp->v_type != VBLK) {
3148 if ((error = VOP_GETATTR(vp, &vattr, p->p_ucred, p)) != 0)
3150 if (p->p_ucred->cr_uid != vattr.va_uid &&
3151 (error = suser_xxx(0, p, PRISON_ROOT)))
3154 VOP_REVOKE(vp, REVOKEALL);
3161 * Convert a user file descriptor to a kernel file entry.
3164 getvnode(fdp, fd, fpp)
3165 struct filedesc *fdp;
3171 if ((u_int)fd >= fdp->fd_nfiles ||
3172 (fp = fdp->fd_ofiles[fd]) == NULL)
3174 if (fp->f_type != DTYPE_VNODE && fp->f_type != DTYPE_FIFO)
3180 * Get (NFS) file handle
3182 #ifndef _SYS_SYSPROTO_H_
3191 register struct getfh_args *uap;
3193 struct nameidata nd;
3195 register struct vnode *vp;
3199 * Must be super user
3204 NDINIT(&nd, LOOKUP, FOLLOW | LOCKLEAF, UIO_USERSPACE, uap->fname, p);
3208 NDFREE(&nd, NDF_ONLY_PNBUF);
3210 bzero(&fh, sizeof(fh));
3211 fh.fh_fsid = vp->v_mount->mnt_stat.f_fsid;
3212 error = VFS_VPTOFH(vp, &fh.fh_fid);
3216 error = copyout(&fh, uap->fhp, sizeof (fh));
3221 * syscall for the rpc.lockd to use to translate a NFS file handle into
3222 * an open descriptor.
3224 * warning: do not remove the suser() call or this becomes one giant
3227 #ifndef _SYS_SYSPROTO_H_
3228 struct fhopen_args {
3229 const struct fhandle *u_fhp;
3236 struct fhopen_args /* {
3237 syscallarg(const struct fhandle *) u_fhp;
3238 syscallarg(int) flags;
3245 struct vattr *vap = &vat;
3248 register struct filedesc *fdp = p->p_fd;
3249 int fmode, mode, error, type;
3254 * Must be super user
3260 fmode = FFLAGS(SCARG(uap, flags));
3261 /* why not allow a non-read/write open for our lockd? */
3262 if (((fmode & (FREAD | FWRITE)) == 0) || (fmode & O_CREAT))
3264 error = copyin(SCARG(uap,u_fhp), &fhp, sizeof(fhp));
3267 /* find the mount point */
3268 mp = vfs_getvfs(&fhp.fh_fsid);
3271 /* now give me my vnode, it gets returned to me locked */
3272 error = VFS_FHTOVP(mp, &fhp.fh_fid, &vp);
3276 * from now on we have to make sure not
3277 * to forget about the vnode
3278 * any error that causes an abort must vput(vp)
3279 * just set error = err and 'goto bad;'.
3285 if (vp->v_type == VLNK) {
3289 if (vp->v_type == VSOCK) {
3294 if (fmode & (FWRITE | O_TRUNC)) {
3295 if (vp->v_type == VDIR) {
3299 error = vn_writechk(vp);
3307 error = VOP_ACCESS(vp, mode, p->p_ucred, p);
3311 if (fmode & O_TRUNC) {
3312 VOP_UNLOCK(vp, 0, p); /* XXX */
3313 VOP_LEASE(vp, p, p->p_ucred, LEASE_WRITE);
3314 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY, p); /* XXX */
3317 error = VOP_SETATTR(vp, vap, p->p_ucred, p);
3321 error = VOP_OPEN(vp, fmode, p->p_ucred, p);
3325 * Make sure that a VM object is created for VMIO support.
3327 if (vn_canvmio(vp) == TRUE) {
3328 if ((error = vfs_object_create(vp, p, p->p_ucred)) != 0)
3335 * end of vn_open code
3338 if ((error = falloc(p, &nfp, &indx)) != 0) {
3346 * hold an extra reference to avoid having fp ripped out
3347 * from under us while we block in the lock op.
3350 nfp->f_data = (caddr_t)vp;
3351 nfp->f_flag = fmode & FMASK;
3352 nfp->f_ops = &vnops;
3353 nfp->f_type = DTYPE_VNODE;
3354 if (fmode & (O_EXLOCK | O_SHLOCK)) {
3355 lf.l_whence = SEEK_SET;
3358 if (fmode & O_EXLOCK)
3359 lf.l_type = F_WRLCK;
3361 lf.l_type = F_RDLCK;
3363 if ((fmode & FNONBLOCK) == 0)
3365 VOP_UNLOCK(vp, 0, p);
3366 if ((error = VOP_ADVLOCK(vp, (caddr_t)fp, F_SETLK, &lf, type)) != 0) {
3368 * lock request failed. Normally close the descriptor
3369 * but handle the case where someone might have dup()d
3370 * or close()d it when we weren't looking.
3372 if (fdp->fd_ofiles[indx] == fp) {
3373 fdp->fd_ofiles[indx] = NULL;
3378 * release our private reference.
3383 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY, p);
3384 fp->f_flag |= FHASLOCK;
3386 if ((vp->v_type == VREG) && (VOP_GETVOBJECT(vp, NULL) != 0))
3387 vfs_object_create(vp, p, p->p_ucred);
3389 VOP_UNLOCK(vp, 0, p);
3391 p->p_retval[0] = indx;
3399 #ifndef _SYS_SYSPROTO_H_
3400 struct fhstat_args {
3401 struct fhandle *u_fhp;
3408 register struct fhstat_args /* {
3409 syscallarg(struct fhandle *) u_fhp;
3410 syscallarg(struct stat *) sb;
3420 * Must be super user
3426 error = copyin(SCARG(uap, u_fhp), &fh, sizeof(fhandle_t));
3430 if ((mp = vfs_getvfs(&fh.fh_fsid)) == NULL)
3432 if ((error = VFS_FHTOVP(mp, &fh.fh_fid, &vp)))
3434 error = vn_stat(vp, &sb, p);
3438 error = copyout(&sb, SCARG(uap, sb), sizeof(sb));
3442 #ifndef _SYS_SYSPROTO_H_
3443 struct fhstatfs_args {
3444 struct fhandle *u_fhp;
3451 struct fhstatfs_args /* {
3452 syscallarg(struct fhandle) *u_fhp;
3453 syscallarg(struct statfs) *buf;
3464 * Must be super user
3466 if ((error = suser(p)))
3469 if ((error = copyin(SCARG(uap, u_fhp), &fh, sizeof(fhandle_t))) != 0)
3472 if ((mp = vfs_getvfs(&fh.fh_fsid)) == NULL)
3474 if ((error = VFS_FHTOVP(mp, &fh.fh_fid, &vp)))
3479 if ((error = VFS_STATFS(mp, sp, p)) != 0)
3481 sp->f_flags = mp->mnt_flag & MNT_VISFLAGMASK;
3482 if (suser_xxx(p->p_ucred, 0, 0)) {
3483 bcopy((caddr_t)sp, (caddr_t)&sb, sizeof(sb));
3484 sb.f_fsid.val[0] = sb.f_fsid.val[1] = 0;
3487 return (copyout(sp, SCARG(uap, buf), sizeof(*sp)));
3491 * Syscall to push extended attribute configuration information into the
3492 * VFS. Accepts a path, which it converts to a mountpoint, as well as
3493 * a command (int cmd), and attribute name and misc data. For now, the
3494 * attribute name is left in userspace for consumption by the VFS_op.
3495 * It will probably be changed to be copied into sysspace by the
3496 * syscall in the future, once issues with various consumers of the
3497 * attribute code have raised their hands.
3499 * Currently this is used only by UFS Extended Attributes.
3504 struct extattrctl_args *uap;
3506 struct nameidata nd;
3510 NDINIT(&nd, LOOKUP, FOLLOW, UIO_USERSPACE, SCARG(uap, path), p);
3511 if ((error = namei(&nd)) != 0)
3513 mp = nd.ni_vp->v_mount;
3515 return (VFS_EXTATTRCTL(mp, SCARG(uap, cmd), SCARG(uap, attrname),
3516 SCARG(uap, arg), p));
3520 * Syscall to set a named extended attribute on a file or directory.
3521 * Accepts attribute name, and a uio structure pointing to the data to set.
3522 * The uio is consumed in the style of writev(). The real work happens
3523 * in VOP_SETEXTATTR().
3526 extattr_set_file(p, uap)
3528 struct extattr_set_file_args *uap;
3530 struct nameidata nd;
3532 struct iovec *iov, *needfree = NULL, aiov[UIO_SMALLIOV];
3533 char attrname[EXTATTR_MAXNAMELEN];
3537 error = copyin(SCARG(uap, attrname), attrname, EXTATTR_MAXNAMELEN);
3540 NDINIT(&nd, LOOKUP, FOLLOW | LOCKLEAF, UIO_USERSPACE,
3541 SCARG(uap, path), p);
3542 if ((error = namei(&nd)) != 0)
3544 iovlen = uap->iovcnt * sizeof(struct iovec);
3545 if (uap->iovcnt > UIO_SMALLIOV) {
3546 if (uap->iovcnt > UIO_MAXIOV) {
3550 MALLOC(iov, struct iovec *, iovlen, M_IOV, M_WAITOK);
3555 auio.uio_iovcnt = uap->iovcnt;
3556 auio.uio_rw = UIO_WRITE;
3557 auio.uio_segflg = UIO_USERSPACE;
3559 auio.uio_offset = 0;
3560 if ((error = copyin((caddr_t)uap->iovp, (caddr_t)iov, iovlen)))
3563 for (i = 0; i < uap->iovcnt; i++) {
3564 if (iov->iov_len > INT_MAX - auio.uio_resid) {
3568 auio.uio_resid += iov->iov_len;
3571 cnt = auio.uio_resid;
3572 error = VOP_SETEXTATTR(nd.ni_vp, attrname, &auio, p->p_cred->pc_ucred,
3574 cnt -= auio.uio_resid;
3575 p->p_retval[0] = cnt;
3578 FREE(needfree, M_IOV);
3584 * Syscall to get a named extended attribute on a file or directory.
3585 * Accepts attribute name, and a uio structure pointing to a buffer for the
3586 * data. The uio is consumed in the style of readv(). The real work
3587 * happens in VOP_GETEXTATTR();
3590 extattr_get_file(p, uap)
3592 struct extattr_get_file_args *uap;
3594 struct nameidata nd;
3596 struct iovec *iov, *needfree, aiov[UIO_SMALLIOV];
3597 char attrname[EXTATTR_MAXNAMELEN];
3601 error = copyin(SCARG(uap, attrname), attrname, EXTATTR_MAXNAMELEN);
3604 NDINIT(&nd, LOOKUP, FOLLOW | LOCKLEAF, UIO_USERSPACE,
3605 SCARG(uap, path), p);
3606 if ((error = namei(&nd)) != 0)
3608 iovlen = uap->iovcnt * sizeof (struct iovec);
3609 if (uap->iovcnt > UIO_SMALLIOV) {
3610 if (uap->iovcnt > UIO_MAXIOV) {
3614 MALLOC(iov, struct iovec *, iovlen, M_IOV, M_WAITOK);
3621 auio.uio_iovcnt = uap->iovcnt;
3622 auio.uio_rw = UIO_READ;
3623 auio.uio_segflg = UIO_USERSPACE;
3625 auio.uio_offset = 0;
3626 if ((error = copyin((caddr_t)uap->iovp, (caddr_t)iov, iovlen)))
3629 for (i = 0; i < uap->iovcnt; i++) {
3630 if (iov->iov_len > INT_MAX - auio.uio_resid) {
3634 auio.uio_resid += iov->iov_len;
3637 cnt = auio.uio_resid;
3638 error = VOP_GETEXTATTR(nd.ni_vp, attrname, &auio, p->p_cred->pc_ucred,
3640 cnt -= auio.uio_resid;
3641 p->p_retval[0] = cnt;
3644 FREE(needfree, M_IOV);
3650 * Syscall to delete a named extended attribute from a file or directory.
3651 * Accepts attribute name. The real work happens in VOP_SETEXTATTR().
3654 extattr_delete_file(p, uap)
3656 struct extattr_delete_file_args *uap;
3658 struct nameidata nd;
3659 char attrname[EXTATTR_MAXNAMELEN];
3662 error = copyin(SCARG(uap, attrname), attrname, EXTATTR_MAXNAMELEN);
3665 NDINIT(&nd, LOOKUP, FOLLOW | LOCKLEAF, UIO_USERSPACE,
3666 SCARG(uap, path), p);
3667 if ((error = namei(&nd)) != 0)
3669 error = VOP_SETEXTATTR(nd.ni_vp, attrname, NULL, p->p_cred->pc_ucred,