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.18 2003/09/28 03:44:02 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/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/namei.h>
63 #include <sys/dirent.h>
64 #include <sys/extattr.h>
66 #include <machine/limits.h>
67 #include <vfs/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 #include <sys/file2.h>
76 static int change_dir (struct nameidata *ndp, struct thread *td);
77 static void checkdirs (struct vnode *olddp);
78 static int chroot_refuse_vdir_fds (struct filedesc *fdp);
79 static int getutimes (const struct timeval *, struct timespec *);
80 static int setfown (struct vnode *, uid_t, gid_t);
81 static int setfmode (struct vnode *, int);
82 static int setfflags (struct vnode *, int);
83 static int setutimes (struct vnode *, const struct timespec *, int);
84 static int usermount = 0; /* if 1, non-root can mount fs. */
86 int (*union_dircheckp) (struct thread *, struct vnode **, struct file *);
88 SYSCTL_INT(_vfs, OID_AUTO, usermount, CTLFLAG_RW, &usermount, 0, "");
91 * Virtual File System System Calls
95 * Mount a file system.
98 * mount_args(char *type, char *path, int flags, caddr_t data)
102 mount(struct mount_args *uap)
104 struct thread *td = curthread;
105 struct proc *p = td->td_proc;
108 struct vfsconf *vfsp;
109 int error, flag = 0, flag2 = 0;
115 char fstypename[MFSNAMELEN];
117 if (usermount == 0 && (error = suser(td)))
120 * Do not allow NFS export by non-root users.
122 if (SCARG(uap, flags) & MNT_EXPORTED) {
128 * Silently enforce MNT_NOSUID and MNT_NODEV for non-root users
131 SCARG(uap, flags) |= MNT_NOSUID | MNT_NODEV;
133 * Get vnode to be covered
135 NDINIT(&nd, NAMEI_LOOKUP, CNP_FOLLOW | CNP_LOCKLEAF, UIO_USERSPACE,
136 SCARG(uap, path), td);
137 if ((error = namei(&nd)) != 0)
139 NDFREE(&nd, NDF_ONLY_PNBUF);
141 if (SCARG(uap, flags) & MNT_UPDATE) {
142 if ((vp->v_flag & VROOT) == 0) {
148 flag2 = mp->mnt_kern_flag;
150 * We only allow the filesystem to be reloaded if it
151 * is currently mounted read-only.
153 if ((SCARG(uap, flags) & MNT_RELOAD) &&
154 ((mp->mnt_flag & MNT_RDONLY) == 0)) {
156 return (EOPNOTSUPP); /* Needs translation */
159 * Only root, or the user that did the original mount is
160 * permitted to update it.
162 if (mp->mnt_stat.f_owner != p->p_ucred->cr_uid &&
163 (error = suser(td))) {
167 if (vfs_busy(mp, LK_NOWAIT, 0, td)) {
171 lwkt_gettoken(&vp->v_interlock);
172 if ((vp->v_flag & VMOUNT) != 0 ||
173 vp->v_mountedhere != NULL) {
174 lwkt_reltoken(&vp->v_interlock);
179 vp->v_flag |= VMOUNT;
180 lwkt_reltoken(&vp->v_interlock);
182 SCARG(uap, flags) & (MNT_RELOAD | MNT_FORCE | MNT_UPDATE);
183 VOP_UNLOCK(vp, 0, td);
187 * If the user is not root, ensure that they own the directory
188 * onto which we are attempting to mount.
190 if ((error = VOP_GETATTR(vp, &va, td)) ||
191 (va.va_uid != p->p_ucred->cr_uid &&
192 (error = suser(td)))) {
196 if ((error = vinvalbuf(vp, V_SAVE, td, 0, 0)) != 0) {
200 if (vp->v_type != VDIR) {
206 * Historically filesystem types were identified by number. If we
207 * get an integer for the filesystem type instead of a string, we
208 * check to see if it matches one of the historic filesystem types.
210 fstypenum = (uintptr_t)SCARG(uap, type);
211 if (fstypenum < maxvfsconf) {
212 for (vfsp = vfsconf; vfsp; vfsp = vfsp->vfc_next)
213 if (vfsp->vfc_typenum == fstypenum)
219 strncpy(fstypename, vfsp->vfc_name, MFSNAMELEN);
221 #endif /* COMPAT_43 */
222 if ((error = copyinstr(SCARG(uap, type), fstypename, MFSNAMELEN, NULL)) != 0) {
226 for (vfsp = vfsconf; vfsp; vfsp = vfsp->vfc_next)
227 if (!strcmp(vfsp->vfc_name, fstypename))
232 /* Only load modules for root (very important!) */
233 if ((error = suser(td)) != 0) {
237 error = linker_load_file(fstypename, &lf);
238 if (error || lf == NULL) {
245 /* lookup again, see if the VFS was loaded */
246 for (vfsp = vfsconf; vfsp; vfsp = vfsp->vfc_next)
247 if (!strcmp(vfsp->vfc_name, fstypename))
251 linker_file_unload(lf);
256 lwkt_gettoken(&vp->v_interlock);
257 if ((vp->v_flag & VMOUNT) != 0 ||
258 vp->v_mountedhere != NULL) {
259 lwkt_reltoken(&vp->v_interlock);
263 vp->v_flag |= VMOUNT;
264 lwkt_reltoken(&vp->v_interlock);
267 * Allocate and initialize the filesystem.
269 mp = malloc(sizeof(struct mount), M_MOUNT, M_WAITOK);
270 bzero((char *)mp, (u_long)sizeof(struct mount));
271 TAILQ_INIT(&mp->mnt_nvnodelist);
272 TAILQ_INIT(&mp->mnt_reservedvnlist);
273 mp->mnt_nvnodelistsize = 0;
274 lockinit(&mp->mnt_lock, 0, "vfslock", 0, LK_NOPAUSE);
275 (void)vfs_busy(mp, LK_NOWAIT, 0, td);
276 mp->mnt_op = vfsp->vfc_vfsops;
278 vfsp->vfc_refcount++;
279 mp->mnt_stat.f_type = vfsp->vfc_typenum;
280 mp->mnt_flag |= vfsp->vfc_flags & MNT_VISFLAGMASK;
281 strncpy(mp->mnt_stat.f_fstypename, vfsp->vfc_name, MFSNAMELEN);
282 mp->mnt_vnodecovered = vp;
283 mp->mnt_stat.f_owner = p->p_ucred->cr_uid;
284 mp->mnt_iosize_max = DFLTPHYS;
285 VOP_UNLOCK(vp, 0, td);
288 * Set the mount level flags.
290 if (SCARG(uap, flags) & MNT_RDONLY)
291 mp->mnt_flag |= MNT_RDONLY;
292 else if (mp->mnt_flag & MNT_RDONLY)
293 mp->mnt_kern_flag |= MNTK_WANTRDWR;
294 mp->mnt_flag &=~ (MNT_NOSUID | MNT_NOEXEC | MNT_NODEV |
295 MNT_SYNCHRONOUS | MNT_UNION | MNT_ASYNC | MNT_NOATIME |
296 MNT_NOSYMFOLLOW | MNT_IGNORE |
297 MNT_NOCLUSTERR | MNT_NOCLUSTERW | MNT_SUIDDIR);
298 mp->mnt_flag |= SCARG(uap, flags) & (MNT_NOSUID | MNT_NOEXEC |
299 MNT_NODEV | MNT_SYNCHRONOUS | MNT_UNION | MNT_ASYNC | MNT_FORCE |
300 MNT_NOSYMFOLLOW | MNT_IGNORE |
301 MNT_NOATIME | MNT_NOCLUSTERR | MNT_NOCLUSTERW | MNT_SUIDDIR);
303 * Mount the filesystem.
304 * XXX The final recipients of VFS_MOUNT just overwrite the ndp they
305 * get. No freeing of cn_pnbuf.
307 error = VFS_MOUNT(mp, SCARG(uap, path), SCARG(uap, data), &nd, td);
308 if (mp->mnt_flag & MNT_UPDATE) {
309 if (mp->mnt_kern_flag & MNTK_WANTRDWR)
310 mp->mnt_flag &= ~MNT_RDONLY;
311 mp->mnt_flag &=~ (MNT_UPDATE | MNT_RELOAD | MNT_FORCE);
312 mp->mnt_kern_flag &=~ MNTK_WANTRDWR;
315 mp->mnt_kern_flag = flag2;
317 if ((mp->mnt_flag & MNT_RDONLY) == 0) {
318 if (mp->mnt_syncer == NULL)
319 error = vfs_allocate_syncvnode(mp);
321 if (mp->mnt_syncer != NULL)
322 vrele(mp->mnt_syncer);
323 mp->mnt_syncer = NULL;
326 lwkt_gettoken(&vp->v_interlock);
327 vp->v_flag &= ~VMOUNT;
328 lwkt_reltoken(&vp->v_interlock);
332 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY, td);
334 * Put the new filesystem on the mount list after root.
338 lwkt_gettoken(&vp->v_interlock);
339 vp->v_flag &= ~VMOUNT;
340 vp->v_mountedhere = mp;
341 lwkt_reltoken(&vp->v_interlock);
342 lwkt_gettoken(&mountlist_token);
343 TAILQ_INSERT_TAIL(&mountlist, mp, mnt_list);
344 lwkt_reltoken(&mountlist_token);
346 VOP_UNLOCK(vp, 0, td);
347 if ((mp->mnt_flag & MNT_RDONLY) == 0)
348 error = vfs_allocate_syncvnode(mp);
350 if ((error = VFS_START(mp, 0, td)) != 0)
353 lwkt_gettoken(&vp->v_interlock);
354 vp->v_flag &= ~VMOUNT;
355 lwkt_reltoken(&vp->v_interlock);
356 mp->mnt_vfc->vfc_refcount--;
358 free((caddr_t)mp, M_MOUNT);
365 * Scan all active processes to see if any of them have a current
366 * or root directory onto which the new filesystem has just been
367 * mounted. If so, replace them with the new mount point.
370 checkdirs(struct vnode *olddp)
372 struct filedesc *fdp;
376 if (olddp->v_usecount == 1)
378 if (VFS_ROOT(olddp->v_mountedhere, &newdp))
379 panic("mount: lost mount");
380 FOREACH_PROC_IN_SYSTEM(p) {
382 if (fdp->fd_cdir == olddp) {
385 fdp->fd_cdir = newdp;
387 if (fdp->fd_rdir == olddp) {
390 fdp->fd_rdir = newdp;
393 if (rootvnode == olddp) {
397 vfs_cache_setroot(rootvnode);
403 * Unmount a file system.
405 * Note: unmount takes a path to the vnode mounted on as argument,
406 * not special file (as before).
409 * umount_args(char *path, int flags)
413 unmount(struct unmount_args *uap)
415 struct thread *td = curthread;
416 struct proc *p = td->td_proc;
423 NDINIT(&nd, NAMEI_LOOKUP, CNP_FOLLOW | CNP_LOCKLEAF, UIO_USERSPACE,
424 SCARG(uap, path), td);
425 if ((error = namei(&nd)) != 0)
428 NDFREE(&nd, NDF_ONLY_PNBUF);
432 * Only root, or the user that did the original mount is
433 * permitted to unmount this filesystem.
435 if ((mp->mnt_stat.f_owner != p->p_ucred->cr_uid) &&
436 (error = suser(td))) {
442 * Don't allow unmounting the root file system.
444 if (mp->mnt_flag & MNT_ROOTFS) {
450 * Must be the root of the filesystem
452 if ((vp->v_flag & VROOT) == 0) {
457 return (dounmount(mp, SCARG(uap, flags), td));
461 * Do the actual file system unmount.
464 dounmount(struct mount *mp, int flags, struct thread *td)
466 struct vnode *coveredvp;
470 lwkt_gettoken(&mountlist_token);
471 if (mp->mnt_kern_flag & MNTK_UNMOUNT) {
472 lwkt_reltoken(&mountlist_token);
475 mp->mnt_kern_flag |= MNTK_UNMOUNT;
476 /* Allow filesystems to detect that a forced unmount is in progress. */
477 if (flags & MNT_FORCE)
478 mp->mnt_kern_flag |= MNTK_UNMOUNTF;
479 error = lockmgr(&mp->mnt_lock, LK_DRAIN | LK_INTERLOCK |
480 ((flags & MNT_FORCE) ? 0 : LK_NOWAIT), &mountlist_token, td);
482 mp->mnt_kern_flag &= ~(MNTK_UNMOUNT | MNTK_UNMOUNTF);
483 if (mp->mnt_kern_flag & MNTK_MWAIT)
488 if (mp->mnt_flag & MNT_EXPUBLIC)
489 vfs_setpublicfs(NULL, NULL, NULL);
491 vfs_msync(mp, MNT_WAIT);
492 async_flag = mp->mnt_flag & MNT_ASYNC;
493 mp->mnt_flag &=~ MNT_ASYNC;
494 cache_purgevfs(mp); /* remove cache entries for this file sys */
495 if (mp->mnt_syncer != NULL)
496 vrele(mp->mnt_syncer);
497 if (((mp->mnt_flag & MNT_RDONLY) ||
498 (error = VFS_SYNC(mp, MNT_WAIT, td)) == 0) ||
500 error = VFS_UNMOUNT(mp, flags, td);
501 lwkt_gettoken(&mountlist_token);
503 if ((mp->mnt_flag & MNT_RDONLY) == 0 && mp->mnt_syncer == NULL)
504 (void) vfs_allocate_syncvnode(mp);
505 mp->mnt_kern_flag &= ~(MNTK_UNMOUNT | MNTK_UNMOUNTF);
506 mp->mnt_flag |= async_flag;
507 lockmgr(&mp->mnt_lock, LK_RELEASE | LK_INTERLOCK | LK_REENABLE,
508 &mountlist_token, td);
509 if (mp->mnt_kern_flag & MNTK_MWAIT)
513 TAILQ_REMOVE(&mountlist, mp, mnt_list);
514 if ((coveredvp = mp->mnt_vnodecovered) != NULLVP) {
515 coveredvp->v_mountedhere = NULL;
518 mp->mnt_vfc->vfc_refcount--;
519 if (!TAILQ_EMPTY(&mp->mnt_nvnodelist))
520 panic("unmount: dangling vnode");
521 lockmgr(&mp->mnt_lock, LK_RELEASE | LK_INTERLOCK, &mountlist_token, td);
522 if (mp->mnt_kern_flag & MNTK_MWAIT)
524 free((caddr_t)mp, M_MOUNT);
529 * Sync each mounted filesystem.
533 static int syncprt = 0;
534 SYSCTL_INT(_debug, OID_AUTO, syncprt, CTLFLAG_RW, &syncprt, 0, "");
539 sync(struct sync_args *uap)
541 struct thread *td = curthread;
542 struct mount *mp, *nmp;
545 lwkt_gettoken(&mountlist_token);
546 for (mp = TAILQ_FIRST(&mountlist); mp != NULL; mp = nmp) {
547 if (vfs_busy(mp, LK_NOWAIT, &mountlist_token, td)) {
548 nmp = TAILQ_NEXT(mp, mnt_list);
551 if ((mp->mnt_flag & MNT_RDONLY) == 0) {
552 asyncflag = mp->mnt_flag & MNT_ASYNC;
553 mp->mnt_flag &= ~MNT_ASYNC;
554 vfs_msync(mp, MNT_NOWAIT);
555 VFS_SYNC(mp, MNT_NOWAIT, td);
556 mp->mnt_flag |= asyncflag;
558 lwkt_gettoken(&mountlist_token);
559 nmp = TAILQ_NEXT(mp, mnt_list);
562 lwkt_reltoken(&mountlist_token);
565 * XXX don't call vfs_bufstats() yet because that routine
566 * was not imported in the Lite2 merge.
571 #endif /* DIAGNOSTIC */
576 /* XXX PRISON: could be per prison flag */
577 static int prison_quotas;
579 SYSCTL_INT(_kern_prison, OID_AUTO, quotas, CTLFLAG_RW, &prison_quotas, 0, "");
583 * quotactl_args(char *path, int fcmd, int uid, caddr_t arg)
585 * Change filesystem quotas.
589 quotactl(struct quotactl_args *uap)
591 struct thread *td = curthread;
592 struct proc *p = td->td_proc;
598 if (p->p_ucred->cr_prison && !prison_quotas)
600 NDINIT(&nd, NAMEI_LOOKUP, CNP_FOLLOW, UIO_USERSPACE,
601 SCARG(uap, path), td);
602 if ((error = namei(&nd)) != 0)
604 mp = nd.ni_vp->v_mount;
605 NDFREE(&nd, NDF_ONLY_PNBUF);
607 return (VFS_QUOTACTL(mp, SCARG(uap, cmd), SCARG(uap, uid),
608 SCARG(uap, arg), td));
612 * statfs_args(char *path, struct statfs *buf)
614 * Get filesystem statistics.
618 statfs(struct statfs_args *uap)
620 struct thread *td = curthread;
627 NDINIT(&nd, NAMEI_LOOKUP, CNP_FOLLOW, UIO_USERSPACE,
628 SCARG(uap, path), td);
629 if ((error = namei(&nd)) != 0)
631 mp = nd.ni_vp->v_mount;
633 NDFREE(&nd, NDF_ONLY_PNBUF);
635 error = VFS_STATFS(mp, sp, td);
638 sp->f_flags = mp->mnt_flag & MNT_VISFLAGMASK;
640 bcopy((caddr_t)sp, (caddr_t)&sb, sizeof(sb));
641 sb.f_fsid.val[0] = sb.f_fsid.val[1] = 0;
644 return (copyout((caddr_t)sp, (caddr_t)SCARG(uap, buf), sizeof(*sp)));
648 * fstatfs_args(int fd, struct statfs *buf)
650 * Get filesystem statistics.
654 fstatfs(struct fstatfs_args *uap)
656 struct thread *td = curthread;
657 struct proc *p = td->td_proc;
665 if ((error = getvnode(p->p_fd, SCARG(uap, fd), &fp)) != 0)
667 mp = ((struct vnode *)fp->f_data)->v_mount;
671 error = VFS_STATFS(mp, sp, td);
674 sp->f_flags = mp->mnt_flag & MNT_VISFLAGMASK;
676 bcopy((caddr_t)sp, (caddr_t)&sb, sizeof(sb));
677 sb.f_fsid.val[0] = sb.f_fsid.val[1] = 0;
680 return (copyout((caddr_t)sp, (caddr_t)SCARG(uap, buf), sizeof(*sp)));
684 * getfsstat_args(struct statfs *buf, long bufsize, int flags)
686 * Get statistics on all filesystems.
690 getfsstat(struct getfsstat_args *uap)
692 struct thread *td = curthread;
693 struct mount *mp, *nmp;
696 long count, maxcount, error;
698 maxcount = SCARG(uap, bufsize) / sizeof(struct statfs);
699 sfsp = (caddr_t)SCARG(uap, buf);
701 lwkt_gettoken(&mountlist_token);
702 for (mp = TAILQ_FIRST(&mountlist); mp != NULL; mp = nmp) {
703 if (vfs_busy(mp, LK_NOWAIT, &mountlist_token, td)) {
704 nmp = TAILQ_NEXT(mp, mnt_list);
707 if (sfsp && count < maxcount) {
710 * If MNT_NOWAIT or MNT_LAZY is specified, do not
711 * refresh the fsstat cache. MNT_NOWAIT or MNT_LAZY
712 * overrides MNT_WAIT.
714 if (((SCARG(uap, flags) & (MNT_LAZY|MNT_NOWAIT)) == 0 ||
715 (SCARG(uap, flags) & MNT_WAIT)) &&
716 (error = VFS_STATFS(mp, sp, td))) {
717 lwkt_gettoken(&mountlist_token);
718 nmp = TAILQ_NEXT(mp, mnt_list);
722 sp->f_flags = mp->mnt_flag & MNT_VISFLAGMASK;
723 error = copyout((caddr_t)sp, sfsp, sizeof(*sp));
731 lwkt_gettoken(&mountlist_token);
732 nmp = TAILQ_NEXT(mp, mnt_list);
735 lwkt_reltoken(&mountlist_token);
736 if (sfsp && count > maxcount)
737 uap->sysmsg_result = maxcount;
739 uap->sysmsg_result = count;
744 * fchdir_args(int fd)
746 * Change current working directory to a given file descriptor.
750 fchdir(struct fchdir_args *uap)
752 struct thread *td = curthread;
753 struct proc *p = td->td_proc;
754 struct filedesc *fdp = p->p_fd;
755 struct vnode *vp, *tdp;
760 if ((error = getvnode(fdp, SCARG(uap, fd), &fp)) != 0)
762 vp = (struct vnode *)fp->f_data;
764 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY, td);
765 if (vp->v_type != VDIR)
768 error = VOP_ACCESS(vp, VEXEC, p->p_ucred, td);
769 while (!error && (mp = vp->v_mountedhere) != NULL) {
770 if (vfs_busy(mp, 0, 0, td))
772 error = VFS_ROOT(mp, &tdp);
783 VOP_UNLOCK(vp, 0, td);
790 * chdir_args(char *path)
792 * Change current working directory (``.'').
796 chdir(struct chdir_args *uap)
798 struct thread *td = curthread;
799 struct proc *p = td->td_proc;
800 struct filedesc *fdp = p->p_fd;
804 NDINIT(&nd, NAMEI_LOOKUP, CNP_FOLLOW | CNP_LOCKLEAF, UIO_USERSPACE,
805 SCARG(uap, path), td);
806 if ((error = change_dir(&nd, td)) != 0)
808 NDFREE(&nd, NDF_ONLY_PNBUF);
810 fdp->fd_cdir = nd.ni_vp;
815 * Helper function for raised chroot(2) security function: Refuse if
816 * any filedescriptors are open directories.
819 chroot_refuse_vdir_fds(fdp)
820 struct filedesc *fdp;
827 for (fd = 0; fd < fdp->fd_nfiles ; fd++) {
828 error = getvnode(fdp, fd, &fp);
831 vp = (struct vnode *)fp->f_data;
832 if (vp->v_type != VDIR)
840 * This sysctl determines if we will allow a process to chroot(2) if it
841 * has a directory open:
842 * 0: disallowed for all processes.
843 * 1: allowed for processes that were not already chroot(2)'ed.
844 * 2: allowed for all processes.
847 static int chroot_allow_open_directories = 1;
849 SYSCTL_INT(_kern, OID_AUTO, chroot_allow_open_directories, CTLFLAG_RW,
850 &chroot_allow_open_directories, 0, "");
853 * chroot_args(char *path)
855 * Change notion of root (``/'') directory.
859 chroot(struct chroot_args *uap)
861 struct thread *td = curthread;
862 struct proc *p = td->td_proc;
863 struct filedesc *fdp = p->p_fd;
868 error = suser_cred(p->p_ucred, PRISON_ROOT);
871 if (chroot_allow_open_directories == 0 ||
872 (chroot_allow_open_directories == 1 && fdp->fd_rdir != rootvnode))
873 error = chroot_refuse_vdir_fds(fdp);
876 NDINIT(&nd, NAMEI_LOOKUP, CNP_FOLLOW | CNP_LOCKLEAF, UIO_USERSPACE,
877 SCARG(uap, path), td);
878 if ((error = change_dir(&nd, td)) != 0)
880 NDFREE(&nd, NDF_ONLY_PNBUF);
882 fdp->fd_rdir = nd.ni_vp;
884 fdp->fd_jdir = nd.ni_vp;
891 * Common routine for chroot and chdir.
894 change_dir(struct nameidata *ndp, struct thread *td)
903 if (vp->v_type != VDIR)
906 error = VOP_ACCESS(vp, VEXEC, ndp->ni_cnd.cn_cred, td);
910 VOP_UNLOCK(vp, 0, td);
915 * open_args(char *path, int flags, int mode)
917 * Check permissions, allocate an open file structure,
918 * and call the device open routine if any.
921 open(struct open_args *uap)
923 struct thread *td = curthread;
924 struct proc *p = td->td_proc;
925 struct filedesc *fdp = p->p_fd;
928 int cmode, flags, oflags;
930 int type, indx, error;
934 oflags = SCARG(uap, flags);
935 if ((oflags & O_ACCMODE) == O_ACCMODE)
937 flags = FFLAGS(oflags);
938 error = falloc(p, &nfp, &indx);
942 cmode = ((SCARG(uap, mode) &~ fdp->fd_cmask) & ALLPERMS) &~ S_ISTXT;
943 NDINIT(&nd, NAMEI_LOOKUP, CNP_FOLLOW, UIO_USERSPACE,
944 SCARG(uap, path), td);
945 p->p_dupfd = -indx - 1; /* XXX check for fdopen */
947 * Bump the ref count to prevent another process from closing
948 * the descriptor while we are blocked in vn_open()
951 error = vn_open(&nd, flags, cmode);
954 * release our own reference
959 * handle special fdopen() case. bleh. dupfdopen() is
960 * responsible for dropping the old contents of ofiles[indx]
963 if ((error == ENODEV || error == ENXIO) &&
964 p->p_dupfd >= 0 && /* XXX from fdopen */
966 dupfdopen(fdp, indx, p->p_dupfd, flags, error)) == 0) {
967 uap->sysmsg_result = indx;
971 * Clean up the descriptor, but only if another thread hadn't
972 * replaced or closed it.
974 if (fdp->fd_ofiles[indx] == fp) {
975 fdp->fd_ofiles[indx] = NULL;
979 if (error == ERESTART)
984 NDFREE(&nd, NDF_ONLY_PNBUF);
988 * There should be 2 references on the file, one from the descriptor
989 * table, and one for us.
991 * Handle the case where someone closed the file (via its file
992 * descriptor) while we were blocked. The end result should look
993 * like opening the file succeeded but it was immediately closed.
995 if (fp->f_count == 1) {
996 KASSERT(fdp->fd_ofiles[indx] != fp,
997 ("Open file descriptor lost all refs"));
998 VOP_UNLOCK(vp, 0, td);
999 vn_close(vp, flags & FMASK, td);
1001 uap->sysmsg_result = indx;
1005 fp->f_data = (caddr_t)vp;
1006 fp->f_flag = flags & FMASK;
1008 fp->f_type = (vp->v_type == VFIFO ? DTYPE_FIFO : DTYPE_VNODE);
1009 if (flags & (O_EXLOCK | O_SHLOCK)) {
1010 lf.l_whence = SEEK_SET;
1013 if (flags & O_EXLOCK)
1014 lf.l_type = F_WRLCK;
1016 lf.l_type = F_RDLCK;
1018 if ((flags & FNONBLOCK) == 0)
1020 VOP_UNLOCK(vp, 0, td);
1021 if ((error = VOP_ADVLOCK(vp, (caddr_t)fp, F_SETLK, &lf, type)) != 0) {
1023 * lock request failed. Normally close the descriptor
1024 * but handle the case where someone might have dup()d
1025 * it when we weren't looking. One reference is
1026 * owned by the descriptor array, the other by us.
1028 if (fdp->fd_ofiles[indx] == fp) {
1029 fdp->fd_ofiles[indx] = NULL;
1035 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY, td);
1036 fp->f_flag |= FHASLOCK;
1038 /* assert that vn_open created a backing object if one is needed */
1039 KASSERT(!vn_canvmio(vp) || VOP_GETVOBJECT(vp, NULL) == 0,
1040 ("open: vmio vnode has no backing object after vn_open"));
1041 VOP_UNLOCK(vp, 0, td);
1044 * release our private reference, leaving the one associated with the
1045 * descriptor table intact.
1048 uap->sysmsg_result = indx;
1054 * ocreat(char *path, int mode)
1059 ocreat(struct ocreat_args *uap)
1061 struct open_args /* {
1062 syscallarg(char *) path;
1063 syscallarg(int) flags;
1064 syscallarg(int) mode;
1067 SCARG(&nuap, path) = SCARG(uap, path);
1068 SCARG(&nuap, mode) = SCARG(uap, mode);
1069 SCARG(&nuap, flags) = O_WRONLY | O_CREAT | O_TRUNC;
1070 return (open(&nuap));
1072 #endif /* COMPAT_43 */
1075 * mknod_args(char *path, int mode, int dev)
1077 * Create a special file.
1081 mknod(struct mknod_args *uap)
1083 struct thread *td = curthread;
1084 struct proc *p = td->td_proc;
1089 struct nameidata nd;
1093 switch (SCARG(uap, mode) & S_IFMT) {
1099 error = suser_cred(p->p_ucred, PRISON_ROOT);
1105 NDINIT(&nd, NAMEI_CREATE, CNP_LOCKPARENT, UIO_USERSPACE,
1106 SCARG(uap, path), td);
1107 if ((error = namei(&nd)) != 0)
1114 vattr.va_mode = (SCARG(uap, mode) & ALLPERMS) &~ p->p_fd->fd_cmask;
1115 vattr.va_rdev = SCARG(uap, dev);
1118 switch (SCARG(uap, mode) & S_IFMT) {
1119 case S_IFMT: /* used by badsect to flag bad sectors */
1120 vattr.va_type = VBAD;
1123 vattr.va_type = VCHR;
1126 vattr.va_type = VBLK;
1137 VOP_LEASE(nd.ni_dvp, td, p->p_ucred, LEASE_WRITE);
1139 error = VOP_WHITEOUT(nd.ni_dvp, &nd.ni_cnd, NAMEI_CREATE);
1141 error = VOP_MKNOD(nd.ni_dvp, &nd.ni_vp,
1142 &nd.ni_cnd, &vattr);
1146 NDFREE(&nd, NDF_ONLY_PNBUF);
1149 NDFREE(&nd, NDF_ONLY_PNBUF);
1150 if (nd.ni_dvp == vp)
1157 ASSERT_VOP_UNLOCKED(nd.ni_dvp, "mknod");
1158 ASSERT_VOP_UNLOCKED(nd.ni_vp, "mknod");
1163 * mkfifo_args(char *path, int mode)
1165 * Create a named pipe.
1169 mkfifo(struct mkfifo_args *uap)
1171 struct thread *td = curthread;
1172 struct proc *p = td->td_proc;
1175 struct nameidata nd;
1178 NDINIT(&nd, NAMEI_CREATE, CNP_LOCKPARENT, UIO_USERSPACE,
1179 SCARG(uap, path), td);
1180 if ((error = namei(&nd)) != 0)
1182 if (nd.ni_vp != NULL) {
1183 NDFREE(&nd, NDF_ONLY_PNBUF);
1184 if (nd.ni_dvp == nd.ni_vp)
1192 vattr.va_type = VFIFO;
1193 vattr.va_mode = (SCARG(uap, mode) & ALLPERMS) &~ p->p_fd->fd_cmask;
1194 VOP_LEASE(nd.ni_dvp, td, p->p_ucred, LEASE_WRITE);
1195 error = VOP_MKNOD(nd.ni_dvp, &nd.ni_vp, &nd.ni_cnd, &vattr);
1198 NDFREE(&nd, NDF_ONLY_PNBUF);
1204 * link_args(char *path, char *link)
1206 * Make a hard file link.
1210 link(struct link_args *uap)
1212 struct thread *td = curthread;
1213 struct proc *p = td->td_proc;
1215 struct nameidata nd;
1219 NDINIT(&nd, NAMEI_LOOKUP, CNP_FOLLOW | CNP_NOOBJ, UIO_USERSPACE,
1220 SCARG(uap, path), td);
1221 if ((error = namei(&nd)) != 0)
1223 NDFREE(&nd, NDF_ONLY_PNBUF);
1225 if (vp->v_type == VDIR)
1226 error = EPERM; /* POSIX */
1228 NDINIT(&nd, NAMEI_CREATE, CNP_LOCKPARENT | CNP_NOOBJ,
1229 UIO_USERSPACE, SCARG(uap, link), td);
1232 if (nd.ni_vp != NULL) {
1237 VOP_LEASE(nd.ni_dvp, td, p->p_ucred,
1239 VOP_LEASE(vp, td, p->p_ucred, LEASE_WRITE);
1240 error = VOP_LINK(nd.ni_dvp, vp, &nd.ni_cnd);
1242 NDFREE(&nd, NDF_ONLY_PNBUF);
1243 if (nd.ni_dvp == nd.ni_vp)
1250 ASSERT_VOP_UNLOCKED(nd.ni_dvp, "link");
1251 ASSERT_VOP_UNLOCKED(nd.ni_vp, "link");
1256 * symlink(char *path, char *link)
1258 * Make a symbolic link.
1262 symlink(struct symlink_args *uap)
1264 struct thread *td = curthread;
1265 struct proc *p = td->td_proc;
1269 struct nameidata nd;
1271 path = zalloc(namei_zone);
1272 if ((error = copyinstr(SCARG(uap, path), path, MAXPATHLEN, NULL)) != 0)
1275 NDINIT(&nd, NAMEI_CREATE, CNP_LOCKPARENT | CNP_NOOBJ, UIO_USERSPACE,
1276 SCARG(uap, link), td);
1277 if ((error = namei(&nd)) != 0)
1280 NDFREE(&nd, NDF_ONLY_PNBUF);
1281 if (nd.ni_dvp == nd.ni_vp)
1290 vattr.va_mode = ACCESSPERMS &~ p->p_fd->fd_cmask;
1291 VOP_LEASE(nd.ni_dvp, td, p->p_ucred, LEASE_WRITE);
1292 error = VOP_SYMLINK(nd.ni_dvp, &nd.ni_vp, &nd.ni_cnd, &vattr, path);
1293 NDFREE(&nd, NDF_ONLY_PNBUF);
1297 ASSERT_VOP_UNLOCKED(nd.ni_dvp, "symlink");
1298 ASSERT_VOP_UNLOCKED(nd.ni_vp, "symlink");
1300 zfree(namei_zone, path);
1305 * undelete_args(char *path)
1307 * Delete a whiteout from the filesystem.
1311 undelete(struct undelete_args *uap)
1313 struct thread *td = curthread;
1314 struct proc *p = td->td_proc;
1316 struct nameidata nd;
1319 NDINIT(&nd, NAMEI_DELETE, CNP_LOCKPARENT | CNP_DOWHITEOUT, UIO_USERSPACE,
1320 SCARG(uap, path), td);
1325 if (nd.ni_vp != NULLVP || !(nd.ni_cnd.cn_flags & CNP_ISWHITEOUT)) {
1326 NDFREE(&nd, NDF_ONLY_PNBUF);
1327 if (nd.ni_dvp == nd.ni_vp)
1336 VOP_LEASE(nd.ni_dvp, td, p->p_ucred, LEASE_WRITE);
1337 error = VOP_WHITEOUT(nd.ni_dvp, &nd.ni_cnd, NAMEI_DELETE);
1338 NDFREE(&nd, NDF_ONLY_PNBUF);
1340 ASSERT_VOP_UNLOCKED(nd.ni_dvp, "undelete");
1341 ASSERT_VOP_UNLOCKED(nd.ni_vp, "undelete");
1346 * unlink_args(char *path)
1348 * Delete a name from the filesystem.
1351 unlink(struct unlink_args *uap)
1353 struct thread *td = curthread;
1354 struct proc *p = td->td_proc;
1357 struct nameidata nd;
1360 NDINIT(&nd, NAMEI_DELETE, CNP_LOCKPARENT, UIO_USERSPACE, SCARG(uap, path), td);
1361 if ((error = namei(&nd)) != 0)
1364 VOP_LEASE(vp, td, p->p_ucred, LEASE_WRITE);
1365 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY, td);
1367 if (vp->v_type == VDIR)
1368 error = EPERM; /* POSIX */
1371 * The root of a mounted filesystem cannot be deleted.
1373 * XXX: can this only be a VDIR case?
1375 if (vp->v_flag & VROOT)
1380 VOP_LEASE(nd.ni_dvp, td, p->p_ucred, LEASE_WRITE);
1381 error = VOP_REMOVE(nd.ni_dvp, vp, &nd.ni_cnd);
1383 NDFREE(&nd, NDF_ONLY_PNBUF);
1384 if (nd.ni_dvp == vp)
1390 ASSERT_VOP_UNLOCKED(nd.ni_dvp, "unlink");
1391 ASSERT_VOP_UNLOCKED(nd.ni_vp, "unlink");
1396 * lseek_args(int fd, int pad, off_t offset, int whence)
1398 * Reposition read/write file offset.
1401 lseek(struct lseek_args *uap)
1403 struct thread *td = curthread;
1404 struct proc *p = td->td_proc;
1405 struct filedesc *fdp = p->p_fd;
1410 if ((u_int)SCARG(uap, fd) >= fdp->fd_nfiles ||
1411 (fp = fdp->fd_ofiles[SCARG(uap, fd)]) == NULL)
1413 if (fp->f_type != DTYPE_VNODE)
1415 switch (SCARG(uap, whence)) {
1417 fp->f_offset += SCARG(uap, offset);
1420 error=VOP_GETATTR((struct vnode *)fp->f_data, &vattr, td);
1423 fp->f_offset = SCARG(uap, offset) + vattr.va_size;
1426 fp->f_offset = SCARG(uap, offset);
1431 uap->sysmsg_offset = fp->f_offset;
1435 #if defined(COMPAT_43) || defined(COMPAT_SUNOS)
1437 * Reposition read/write file offset.
1439 * olseek_args(int fd, long offset, int whence)
1442 olseek(struct olseek_args *uap)
1444 struct lseek_args /* {
1446 syscallarg(int) pad;
1447 syscallarg(off_t) offset;
1448 syscallarg(int) whence;
1452 SCARG(&nuap, fd) = SCARG(uap, fd);
1453 SCARG(&nuap, offset) = SCARG(uap, offset);
1454 SCARG(&nuap, whence) = SCARG(uap, whence);
1455 error = lseek(&nuap);
1458 #endif /* COMPAT_43 */
1461 * access_args(char *path, int flags)
1463 * Check access permissions.
1466 access(struct access_args *uap)
1468 struct thread *td = curthread;
1469 struct proc *p = td->td_proc;
1470 struct ucred *cred, *tmpcred;
1473 struct nameidata nd;
1477 * Create and modify a temporary credential instead of one that
1478 * is potentially shared. This could also mess up socket
1479 * buffer accounting which can run in an interrupt context.
1481 tmpcred = crdup(cred);
1482 tmpcred->cr_uid = p->p_ucred->cr_ruid;
1483 tmpcred->cr_groups[0] = p->p_ucred->cr_rgid;
1484 p->p_ucred = tmpcred;
1485 NDINIT(&nd, NAMEI_LOOKUP, CNP_FOLLOW | CNP_LOCKLEAF | CNP_NOOBJ,
1486 UIO_USERSPACE, SCARG(uap, path), td);
1487 if ((error = namei(&nd)) != 0)
1491 /* Flags == 0 means only check for existence. */
1492 if (SCARG(uap, flags)) {
1494 if (SCARG(uap, flags) & R_OK)
1496 if (SCARG(uap, flags) & W_OK)
1498 if (SCARG(uap, flags) & X_OK)
1500 if ((flags & VWRITE) == 0 || (error = vn_writechk(vp)) == 0)
1501 error = VOP_ACCESS(vp, flags, tmpcred, td);
1503 NDFREE(&nd, NDF_ONLY_PNBUF);
1511 #if defined(COMPAT_43) || defined(COMPAT_SUNOS)
1513 * ostat_args(char *path, struct ostat *ub)
1515 * Get file status; this version follows links.
1519 ostat(struct ostat_args *uap)
1521 struct thread *td = curthread;
1525 struct nameidata nd;
1527 NDINIT(&nd, NAMEI_LOOKUP, CNP_FOLLOW | CNP_LOCKLEAF | CNP_NOOBJ,
1528 UIO_USERSPACE, SCARG(uap, path), td);
1529 if ((error = namei(&nd)) != 0)
1531 NDFREE(&nd, NDF_ONLY_PNBUF);
1532 error = vn_stat(nd.ni_vp, &sb, td);
1537 error = copyout((caddr_t)&osb, (caddr_t)SCARG(uap, ub), sizeof (osb));
1542 * olstat_args(char *path, struct ostat *ub)
1544 * Get file status; this version does not follow links.
1548 olstat(struct olstat_args *uap)
1550 struct thread *td = curthread;
1555 struct nameidata nd;
1557 NDINIT(&nd, NAMEI_LOOKUP, CNP_NOFOLLOW | CNP_LOCKLEAF | CNP_NOOBJ,
1558 UIO_USERSPACE, SCARG(uap, path), td);
1559 if ((error = namei(&nd)) != 0)
1562 error = vn_stat(vp, &sb, td);
1563 NDFREE(&nd, NDF_ONLY_PNBUF);
1568 error = copyout((caddr_t)&osb, (caddr_t)SCARG(uap, ub), sizeof (osb));
1573 * Convert from an old to a new stat structure.
1580 ost->st_dev = st->st_dev;
1581 ost->st_ino = st->st_ino;
1582 ost->st_mode = st->st_mode;
1583 ost->st_nlink = st->st_nlink;
1584 ost->st_uid = st->st_uid;
1585 ost->st_gid = st->st_gid;
1586 ost->st_rdev = st->st_rdev;
1587 if (st->st_size < (quad_t)1 << 32)
1588 ost->st_size = st->st_size;
1591 ost->st_atime = st->st_atime;
1592 ost->st_mtime = st->st_mtime;
1593 ost->st_ctime = st->st_ctime;
1594 ost->st_blksize = st->st_blksize;
1595 ost->st_blocks = st->st_blocks;
1596 ost->st_flags = st->st_flags;
1597 ost->st_gen = st->st_gen;
1599 #endif /* COMPAT_43 || COMPAT_SUNOS */
1602 * stat_args(char *path, struct stat *ub)
1604 * Get file status; this version follows links.
1608 stat(struct stat_args *uap)
1610 struct thread *td = curthread;
1613 struct nameidata nd;
1615 NDINIT(&nd, NAMEI_LOOKUP, CNP_FOLLOW | CNP_LOCKLEAF | CNP_NOOBJ,
1616 UIO_USERSPACE, SCARG(uap, path), td);
1617 if ((error = namei(&nd)) != 0)
1619 error = vn_stat(nd.ni_vp, &sb, td);
1620 NDFREE(&nd, NDF_ONLY_PNBUF);
1624 error = copyout((caddr_t)&sb, (caddr_t)SCARG(uap, ub), sizeof (sb));
1629 * lstat_args(char *path, struct stat *ub)
1631 * Get file status; this version does not follow links.
1635 lstat(struct lstat_args *uap)
1637 struct thread *td = curthread;
1641 struct nameidata nd;
1643 NDINIT(&nd, NAMEI_LOOKUP, CNP_NOFOLLOW | CNP_LOCKLEAF | CNP_NOOBJ,
1644 UIO_USERSPACE, SCARG(uap, path), td);
1645 if ((error = namei(&nd)) != 0)
1648 error = vn_stat(vp, &sb, td);
1649 NDFREE(&nd, NDF_ONLY_PNBUF);
1653 error = copyout((caddr_t)&sb, (caddr_t)SCARG(uap, ub), sizeof (sb));
1662 nsb->st_dev = sb->st_dev;
1663 nsb->st_ino = sb->st_ino;
1664 nsb->st_mode = sb->st_mode;
1665 nsb->st_nlink = sb->st_nlink;
1666 nsb->st_uid = sb->st_uid;
1667 nsb->st_gid = sb->st_gid;
1668 nsb->st_rdev = sb->st_rdev;
1669 nsb->st_atimespec = sb->st_atimespec;
1670 nsb->st_mtimespec = sb->st_mtimespec;
1671 nsb->st_ctimespec = sb->st_ctimespec;
1672 nsb->st_size = sb->st_size;
1673 nsb->st_blocks = sb->st_blocks;
1674 nsb->st_blksize = sb->st_blksize;
1675 nsb->st_flags = sb->st_flags;
1676 nsb->st_gen = sb->st_gen;
1677 nsb->st_qspare[0] = sb->st_qspare[0];
1678 nsb->st_qspare[1] = sb->st_qspare[1];
1682 * nstat_args(char *path, struct nstat *ub)
1686 nstat(struct nstat_args *uap)
1688 struct thread *td = curthread;
1692 struct nameidata nd;
1694 NDINIT(&nd, NAMEI_LOOKUP, CNP_FOLLOW | CNP_LOCKLEAF | CNP_NOOBJ,
1695 UIO_USERSPACE, SCARG(uap, path), td);
1696 if ((error = namei(&nd)) != 0)
1698 NDFREE(&nd, NDF_ONLY_PNBUF);
1699 error = vn_stat(nd.ni_vp, &sb, td);
1703 cvtnstat(&sb, &nsb);
1704 error = copyout((caddr_t)&nsb, (caddr_t)SCARG(uap, ub), sizeof (nsb));
1709 * lstat_args(char *path, struct stat *ub)
1711 * Get file status; this version does not follow links.
1715 nlstat(struct nlstat_args *uap)
1717 struct thread *td = curthread;
1722 struct nameidata nd;
1724 NDINIT(&nd, NAMEI_LOOKUP, CNP_NOFOLLOW | CNP_LOCKLEAF | CNP_NOOBJ,
1725 UIO_USERSPACE, SCARG(uap, path), td);
1726 if ((error = namei(&nd)) != 0)
1729 NDFREE(&nd, NDF_ONLY_PNBUF);
1730 error = vn_stat(vp, &sb, td);
1734 cvtnstat(&sb, &nsb);
1735 error = copyout((caddr_t)&nsb, (caddr_t)SCARG(uap, ub), sizeof (nsb));
1740 * pathconf_Args(char *path, int name)
1742 * Get configurable pathname variables.
1746 pathconf(struct pathconf_args *uap)
1748 struct thread *td = curthread;
1750 struct nameidata nd;
1752 NDINIT(&nd, NAMEI_LOOKUP, CNP_FOLLOW | CNP_LOCKLEAF | CNP_NOOBJ,
1753 UIO_USERSPACE, SCARG(uap, path), td);
1754 if ((error = namei(&nd)) != 0)
1756 NDFREE(&nd, NDF_ONLY_PNBUF);
1757 error = VOP_PATHCONF(nd.ni_vp, SCARG(uap, name), uap->sysmsg_fds);
1763 * readlink_args(char *path, char *buf, int count)
1765 * Return target name of a symbolic link.
1769 readlink(struct readlink_args *uap)
1771 struct thread *td = curthread;
1772 struct proc *p = td->td_proc;
1777 struct nameidata nd;
1779 NDINIT(&nd, NAMEI_LOOKUP, CNP_NOFOLLOW | CNP_LOCKLEAF | CNP_NOOBJ,
1780 UIO_USERSPACE, SCARG(uap, path), td);
1781 if ((error = namei(&nd)) != 0)
1783 NDFREE(&nd, NDF_ONLY_PNBUF);
1785 if (vp->v_type != VLNK)
1788 aiov.iov_base = SCARG(uap, buf);
1789 aiov.iov_len = SCARG(uap, count);
1790 auio.uio_iov = &aiov;
1791 auio.uio_iovcnt = 1;
1792 auio.uio_offset = 0;
1793 auio.uio_rw = UIO_READ;
1794 auio.uio_segflg = UIO_USERSPACE;
1796 auio.uio_resid = SCARG(uap, count);
1797 error = VOP_READLINK(vp, &auio, p->p_ucred);
1800 uap->sysmsg_result = SCARG(uap, count) - auio.uio_resid;
1805 setfflags(struct vnode *vp, int flags)
1807 struct thread *td = curthread;
1808 struct proc *p = td->td_proc;
1813 * Prevent non-root users from setting flags on devices. When
1814 * a device is reused, users can retain ownership of the device
1815 * if they are allowed to set flags and programs assume that
1816 * chown can't fail when done as root.
1818 if ((vp->v_type == VCHR || vp->v_type == VBLK) &&
1819 ((error = suser_cred(p->p_ucred, PRISON_ROOT)) != 0))
1822 VOP_LEASE(vp, td, p->p_ucred, LEASE_WRITE);
1823 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY, td);
1825 vattr.va_flags = flags;
1826 error = VOP_SETATTR(vp, &vattr, p->p_ucred, td);
1827 VOP_UNLOCK(vp, 0, td);
1832 * chflags(char *path, int flags)
1834 * Change flags of a file given a path name.
1838 chflags(struct chflags_args *uap)
1840 struct thread *td = curthread;
1842 struct nameidata nd;
1844 NDINIT(&nd, NAMEI_LOOKUP, CNP_FOLLOW, UIO_USERSPACE,
1845 SCARG(uap, path), td);
1846 if ((error = namei(&nd)) != 0)
1848 NDFREE(&nd, NDF_ONLY_PNBUF);
1849 error = setfflags(nd.ni_vp, SCARG(uap, flags));
1855 * fchflags_args(int fd, int flags)
1857 * Change flags of a file given a file descriptor.
1861 fchflags(struct fchflags_args *uap)
1863 struct thread *td = curthread;
1864 struct proc *p = td->td_proc;
1868 if ((error = getvnode(p->p_fd, SCARG(uap, fd), &fp)) != 0)
1870 return setfflags((struct vnode *) fp->f_data, SCARG(uap, flags));
1874 setfmode(struct vnode *vp, int mode)
1876 struct thread *td = curthread;
1877 struct proc *p = td->td_proc;
1881 VOP_LEASE(vp, td, p->p_ucred, LEASE_WRITE);
1882 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY, td);
1884 vattr.va_mode = mode & ALLPERMS;
1885 error = VOP_SETATTR(vp, &vattr, p->p_ucred, td);
1886 VOP_UNLOCK(vp, 0, td);
1891 * chmod_args(char *path, int mode)
1893 * Change mode of a file given path name.
1897 chmod(struct chmod_args *uap)
1899 struct thread *td = curthread;
1901 struct nameidata nd;
1903 NDINIT(&nd, NAMEI_LOOKUP, CNP_FOLLOW, UIO_USERSPACE,
1904 SCARG(uap, path), td);
1905 if ((error = namei(&nd)) != 0)
1907 NDFREE(&nd, NDF_ONLY_PNBUF);
1908 error = setfmode(nd.ni_vp, SCARG(uap, mode));
1914 * lchmod_args(char *path, int mode)
1916 * Change mode of a file given path name (don't follow links.)
1920 lchmod(struct lchmod_args *uap)
1922 struct thread *td = curthread;
1924 struct nameidata nd;
1926 NDINIT(&nd, NAMEI_LOOKUP, CNP_NOFOLLOW, UIO_USERSPACE,
1927 SCARG(uap, path), td);
1928 if ((error = namei(&nd)) != 0)
1930 NDFREE(&nd, NDF_ONLY_PNBUF);
1931 error = setfmode(nd.ni_vp, SCARG(uap, mode));
1937 * fchmod_args(int fd, int mode)
1939 * Change mode of a file given a file descriptor.
1943 fchmod(struct fchmod_args *uap)
1945 struct thread *td = curthread;
1946 struct proc *p = td->td_proc;
1950 if ((error = getvnode(p->p_fd, SCARG(uap, fd), &fp)) != 0)
1952 return setfmode((struct vnode *)fp->f_data, SCARG(uap, mode));
1956 setfown(struct vnode *vp, uid_t uid, gid_t gid)
1958 struct thread *td = curthread;
1959 struct proc *p = td->td_proc;
1963 VOP_LEASE(vp, td, p->p_ucred, LEASE_WRITE);
1964 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY, td);
1968 error = VOP_SETATTR(vp, &vattr, p->p_ucred, td);
1969 VOP_UNLOCK(vp, 0, td);
1974 * chown(char *path, int uid, int gid)
1976 * Set ownership given a path name.
1980 chown(struct chown_args *uap)
1982 struct thread *td = curthread;
1984 struct nameidata nd;
1986 NDINIT(&nd, NAMEI_LOOKUP, CNP_FOLLOW, UIO_USERSPACE,
1987 SCARG(uap, path), td);
1988 if ((error = namei(&nd)) != 0)
1990 NDFREE(&nd, NDF_ONLY_PNBUF);
1991 error = setfown(nd.ni_vp, SCARG(uap, uid), SCARG(uap, gid));
1997 * lchown_args(char *path, int uid, int gid)
1999 * Set ownership given a path name, do not cross symlinks.
2003 lchown(struct lchown_args *uap)
2005 struct thread *td = curthread;
2007 struct nameidata nd;
2009 NDINIT(&nd, NAMEI_LOOKUP, CNP_NOFOLLOW, UIO_USERSPACE,
2010 SCARG(uap, path), td);
2011 if ((error = namei(&nd)) != 0)
2013 NDFREE(&nd, NDF_ONLY_PNBUF);
2014 error = setfown(nd.ni_vp, SCARG(uap, uid), SCARG(uap, gid));
2020 * fchown_args(int fd, int uid, int gid)
2022 * Set ownership given a file descriptor.
2026 fchown(struct fchown_args *uap)
2028 struct thread *td = curthread;
2029 struct proc *p = td->td_proc;
2033 if ((error = getvnode(p->p_fd, SCARG(uap, fd), &fp)) != 0)
2035 return setfown((struct vnode *)fp->f_data,
2036 SCARG(uap, uid), SCARG(uap, gid));
2040 getutimes(const struct timeval *usrtvp, struct timespec *tsp)
2042 struct timeval tv[2];
2045 if (usrtvp == NULL) {
2047 TIMEVAL_TO_TIMESPEC(&tv[0], &tsp[0]);
2050 if ((error = copyin(usrtvp, tv, sizeof (tv))) != 0)
2052 TIMEVAL_TO_TIMESPEC(&tv[0], &tsp[0]);
2053 TIMEVAL_TO_TIMESPEC(&tv[1], &tsp[1]);
2059 setutimes(struct vnode *vp, const struct timespec *ts, int nullflag)
2061 struct thread *td = curthread;
2062 struct proc *p = td->td_proc;
2066 VOP_LEASE(vp, td, p->p_ucred, LEASE_WRITE);
2067 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY, td);
2069 vattr.va_atime = ts[0];
2070 vattr.va_mtime = ts[1];
2072 vattr.va_vaflags |= VA_UTIMES_NULL;
2073 error = VOP_SETATTR(vp, &vattr, p->p_ucred, td);
2074 VOP_UNLOCK(vp, 0, td);
2079 * utimes_args(char *path, struct timeval *tptr)
2081 * Set the access and modification times of a file.
2085 utimes(struct utimes_args *uap)
2087 struct thread *td = curthread;
2088 struct timespec ts[2];
2089 struct timeval *usrtvp;
2091 struct nameidata nd;
2093 usrtvp = SCARG(uap, tptr);
2094 if ((error = getutimes(usrtvp, ts)) != 0)
2096 NDINIT(&nd, NAMEI_LOOKUP, CNP_FOLLOW, UIO_USERSPACE, SCARG(uap, path), td);
2097 if ((error = namei(&nd)) != 0)
2099 NDFREE(&nd, NDF_ONLY_PNBUF);
2100 error = setutimes(nd.ni_vp, ts, usrtvp == NULL);
2106 * lutimes_args(char *path, struct timeval *tptr)
2108 * Set the access and modification times of a file.
2112 lutimes(struct lutimes_args *uap)
2114 struct thread *td = curthread;
2115 struct timespec ts[2];
2116 struct timeval *usrtvp;
2118 struct nameidata nd;
2120 usrtvp = SCARG(uap, tptr);
2121 if ((error = getutimes(usrtvp, ts)) != 0)
2123 NDINIT(&nd, NAMEI_LOOKUP, CNP_NOFOLLOW, UIO_USERSPACE, SCARG(uap, path), td);
2124 if ((error = namei(&nd)) != 0)
2126 NDFREE(&nd, NDF_ONLY_PNBUF);
2127 error = setutimes(nd.ni_vp, ts, usrtvp == NULL);
2133 * futimes_args(int fd, struct timeval *tptr)
2135 * Set the access and modification times of a file.
2139 futimes(struct futimes_args *uap)
2141 struct thread *td = curthread;
2142 struct proc *p = td->td_proc;
2143 struct timespec ts[2];
2145 struct timeval *usrtvp;
2148 usrtvp = SCARG(uap, tptr);
2149 if ((error = getutimes(usrtvp, ts)) != 0)
2151 if ((error = getvnode(p->p_fd, SCARG(uap, fd), &fp)) != 0)
2153 return setutimes((struct vnode *)fp->f_data, ts, usrtvp == NULL);
2157 * truncate(char *path, int pad, off_t length)
2159 * Truncate a file given its path name.
2163 truncate(struct truncate_args *uap)
2165 struct thread *td = curthread;
2166 struct proc *p = td->td_proc;
2170 struct nameidata nd;
2172 if (uap->length < 0)
2174 NDINIT(&nd, NAMEI_LOOKUP, CNP_FOLLOW, UIO_USERSPACE, SCARG(uap, path), td);
2175 if ((error = namei(&nd)) != 0)
2178 NDFREE(&nd, NDF_ONLY_PNBUF);
2179 VOP_LEASE(vp, td, p->p_ucred, LEASE_WRITE);
2180 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY, td);
2181 if (vp->v_type == VDIR)
2183 else if ((error = vn_writechk(vp)) == 0 &&
2184 (error = VOP_ACCESS(vp, VWRITE, p->p_ucred, td)) == 0) {
2186 vattr.va_size = SCARG(uap, length);
2187 error = VOP_SETATTR(vp, &vattr, p->p_ucred, td);
2194 * ftruncate_args(int fd, int pad, off_t length)
2196 * Truncate a file given a file descriptor.
2200 ftruncate(struct ftruncate_args *uap)
2202 struct thread *td = curthread;
2203 struct proc *p = td->td_proc;
2209 if (uap->length < 0)
2211 if ((error = getvnode(p->p_fd, SCARG(uap, fd), &fp)) != 0)
2213 if ((fp->f_flag & FWRITE) == 0)
2215 vp = (struct vnode *)fp->f_data;
2216 VOP_LEASE(vp, td, p->p_ucred, LEASE_WRITE);
2217 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY, td);
2218 if (vp->v_type == VDIR)
2220 else if ((error = vn_writechk(vp)) == 0) {
2222 vattr.va_size = SCARG(uap, length);
2223 error = VOP_SETATTR(vp, &vattr, fp->f_cred, td);
2225 VOP_UNLOCK(vp, 0, td);
2229 #if defined(COMPAT_43) || defined(COMPAT_SUNOS)
2231 * otruncate_args(char *path, long length)
2233 * Truncate a file given its path name.
2237 otruncate(struct otruncate_args *uap)
2239 struct truncate_args /* {
2240 syscallarg(char *) path;
2241 syscallarg(int) pad;
2242 syscallarg(off_t) length;
2245 SCARG(&nuap, path) = SCARG(uap, path);
2246 SCARG(&nuap, length) = SCARG(uap, length);
2247 return (truncate(&nuap));
2251 * oftruncate_args(int fd, long length)
2253 * Truncate a file given a file descriptor.
2257 oftruncate(struct oftruncate_args *uap)
2259 struct ftruncate_args /* {
2261 syscallarg(int) pad;
2262 syscallarg(off_t) length;
2265 SCARG(&nuap, fd) = SCARG(uap, fd);
2266 SCARG(&nuap, length) = SCARG(uap, length);
2267 return (ftruncate(&nuap));
2269 #endif /* COMPAT_43 || COMPAT_SUNOS */
2274 * Sync an open file.
2278 fsync(struct fsync_args *uap)
2280 struct thread *td = curthread;
2281 struct proc *p = td->td_proc;
2287 if ((error = getvnode(p->p_fd, SCARG(uap, fd), &fp)) != 0)
2289 vp = (struct vnode *)fp->f_data;
2290 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY, td);
2291 if (VOP_GETVOBJECT(vp, &obj) == 0)
2292 vm_object_page_clean(obj, 0, 0, 0);
2293 if ((error = VOP_FSYNC(vp, MNT_WAIT, td)) == 0 &&
2294 vp->v_mount && (vp->v_mount->mnt_flag & MNT_SOFTDEP) &&
2296 error = (*bioops.io_fsync)(vp);
2297 VOP_UNLOCK(vp, 0, td);
2302 * rename_args(char *from, char *to)
2304 * Rename files. Source and destination must either both be directories,
2305 * or both not be directories. If target is a directory, it must be empty.
2309 rename(struct rename_args *uap)
2311 struct thread *td = curthread;
2312 struct proc *p = td->td_proc;
2313 struct vnode *tvp, *fvp, *tdvp;
2314 struct nameidata fromnd, tond;
2318 NDINIT(&fromnd, NAMEI_DELETE, CNP_WANTPARENT | CNP_SAVESTART,
2319 UIO_USERSPACE, SCARG(uap, from), td);
2320 if ((error = namei(&fromnd)) != 0)
2323 NDINIT(&tond, NAMEI_RENAME,
2324 CNP_LOCKPARENT | CNP_LOCKLEAF | CNP_NOCACHE |
2325 CNP_SAVESTART | CNP_NOOBJ,
2326 UIO_USERSPACE, SCARG(uap, to), td);
2327 if (fromnd.ni_vp->v_type == VDIR)
2328 tond.ni_cnd.cn_flags |= CNP_WILLBEDIR;
2329 if ((error = namei(&tond)) != 0) {
2330 /* Translate error code for rename("dir1", "dir2/."). */
2331 if (error == EISDIR && fvp->v_type == VDIR)
2333 NDFREE(&fromnd, NDF_ONLY_PNBUF);
2334 vrele(fromnd.ni_dvp);
2341 if (fvp->v_type == VDIR && tvp->v_type != VDIR) {
2344 } else if (fvp->v_type != VDIR && tvp->v_type == VDIR) {
2352 * If the source is the same as the destination (that is, if they
2353 * are links to the same vnode), then there is nothing to do.
2359 VOP_LEASE(tdvp, td, p->p_ucred, LEASE_WRITE);
2360 if (fromnd.ni_dvp != tdvp) {
2361 VOP_LEASE(fromnd.ni_dvp, td, p->p_ucred, LEASE_WRITE);
2364 VOP_LEASE(tvp, td, p->p_ucred, LEASE_WRITE);
2366 error = VOP_RENAME(fromnd.ni_dvp, fromnd.ni_vp, &fromnd.ni_cnd,
2367 tond.ni_dvp, tond.ni_vp, &tond.ni_cnd);
2368 NDFREE(&fromnd, NDF_ONLY_PNBUF);
2369 NDFREE(&tond, NDF_ONLY_PNBUF);
2371 NDFREE(&fromnd, NDF_ONLY_PNBUF);
2372 NDFREE(&tond, NDF_ONLY_PNBUF);
2379 vrele(fromnd.ni_dvp);
2382 vrele(tond.ni_startdir);
2383 ASSERT_VOP_UNLOCKED(fromnd.ni_dvp, "rename");
2384 ASSERT_VOP_UNLOCKED(fromnd.ni_vp, "rename");
2385 ASSERT_VOP_UNLOCKED(tond.ni_dvp, "rename");
2386 ASSERT_VOP_UNLOCKED(tond.ni_vp, "rename");
2388 if (fromnd.ni_startdir)
2389 vrele(fromnd.ni_startdir);
2396 * mkdir_args(char *path, int mode)
2398 * Make a directory file.
2402 mkdir(struct mkdir_args *uap)
2404 struct thread *td = curthread;
2405 struct proc *p = td->td_proc;
2409 struct nameidata nd;
2412 NDINIT(&nd, NAMEI_CREATE, CNP_LOCKPARENT, UIO_USERSPACE,
2413 SCARG(uap, path), td);
2414 nd.ni_cnd.cn_flags |= CNP_WILLBEDIR;
2415 if ((error = namei(&nd)) != 0)
2419 NDFREE(&nd, NDF_ONLY_PNBUF);
2420 if (nd.ni_dvp == vp)
2428 vattr.va_type = VDIR;
2429 vattr.va_mode = (SCARG(uap, mode) & ACCESSPERMS) &~ p->p_fd->fd_cmask;
2430 VOP_LEASE(nd.ni_dvp, td, p->p_ucred, LEASE_WRITE);
2431 error = VOP_MKDIR(nd.ni_dvp, &nd.ni_vp, &nd.ni_cnd, &vattr);
2432 NDFREE(&nd, NDF_ONLY_PNBUF);
2436 ASSERT_VOP_UNLOCKED(nd.ni_dvp, "mkdir");
2437 ASSERT_VOP_UNLOCKED(nd.ni_vp, "mkdir");
2442 * rmdir_args(char *path)
2444 * Remove a directory file.
2448 rmdir(struct rmdir_args *uap)
2450 struct thread *td = curthread;
2451 struct proc *p = td->td_proc;
2454 struct nameidata nd;
2457 NDINIT(&nd, NAMEI_DELETE, CNP_LOCKPARENT | CNP_LOCKLEAF, UIO_USERSPACE,
2458 SCARG(uap, path), td);
2459 if ((error = namei(&nd)) != 0)
2462 if (vp->v_type != VDIR) {
2467 * No rmdir "." please.
2469 if (nd.ni_dvp == vp) {
2474 * The root of a mounted filesystem cannot be deleted.
2476 if (vp->v_flag & VROOT)
2479 VOP_LEASE(nd.ni_dvp, td, p->p_ucred, LEASE_WRITE);
2480 VOP_LEASE(vp, td, p->p_ucred, LEASE_WRITE);
2481 error = VOP_RMDIR(nd.ni_dvp, nd.ni_vp, &nd.ni_cnd);
2484 NDFREE(&nd, NDF_ONLY_PNBUF);
2485 if (nd.ni_dvp == vp)
2491 ASSERT_VOP_UNLOCKED(nd.ni_dvp, "rmdir");
2492 ASSERT_VOP_UNLOCKED(nd.ni_vp, "rmdir");
2498 * ogetdirentries_args(int fd, char *buf, u_int count, long *basep)
2500 * Read a block of directory entries in a file system independent format.
2503 ogetdirentries(struct ogetdirentries_args *uap)
2505 struct thread *td = curthread;
2506 struct proc *p = td->td_proc;
2509 struct uio auio, kuio;
2510 struct iovec aiov, kiov;
2511 struct dirent *dp, *edp;
2513 int error, eofflag, readcnt;
2516 /* XXX arbitrary sanity limit on `count'. */
2517 if (SCARG(uap, count) > 64 * 1024)
2519 if ((error = getvnode(p->p_fd, SCARG(uap, fd), &fp)) != 0)
2521 if ((fp->f_flag & FREAD) == 0)
2523 vp = (struct vnode *)fp->f_data;
2525 if (vp->v_type != VDIR)
2527 aiov.iov_base = SCARG(uap, buf);
2528 aiov.iov_len = SCARG(uap, count);
2529 auio.uio_iov = &aiov;
2530 auio.uio_iovcnt = 1;
2531 auio.uio_rw = UIO_READ;
2532 auio.uio_segflg = UIO_USERSPACE;
2534 auio.uio_resid = SCARG(uap, count);
2535 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY, td);
2536 loff = auio.uio_offset = fp->f_offset;
2537 # if (BYTE_ORDER != LITTLE_ENDIAN)
2538 if (vp->v_mount->mnt_maxsymlinklen <= 0) {
2539 error = VOP_READDIR(vp, &auio, fp->f_cred, &eofflag,
2541 fp->f_offset = auio.uio_offset;
2546 kuio.uio_iov = &kiov;
2547 kuio.uio_segflg = UIO_SYSSPACE;
2548 kiov.iov_len = SCARG(uap, count);
2549 MALLOC(dirbuf, caddr_t, SCARG(uap, count), M_TEMP, M_WAITOK);
2550 kiov.iov_base = dirbuf;
2551 error = VOP_READDIR(vp, &kuio, fp->f_cred, &eofflag,
2553 fp->f_offset = kuio.uio_offset;
2555 readcnt = SCARG(uap, count) - kuio.uio_resid;
2556 edp = (struct dirent *)&dirbuf[readcnt];
2557 for (dp = (struct dirent *)dirbuf; dp < edp; ) {
2558 # if (BYTE_ORDER == LITTLE_ENDIAN)
2560 * The expected low byte of
2561 * dp->d_namlen is our dp->d_type.
2562 * The high MBZ byte of dp->d_namlen
2563 * is our dp->d_namlen.
2565 dp->d_type = dp->d_namlen;
2569 * The dp->d_type is the high byte
2570 * of the expected dp->d_namlen,
2571 * so must be zero'ed.
2575 if (dp->d_reclen > 0) {
2576 dp = (struct dirent *)
2577 ((char *)dp + dp->d_reclen);
2584 error = uiomove(dirbuf, readcnt, &auio);
2586 FREE(dirbuf, M_TEMP);
2588 VOP_UNLOCK(vp, 0, td);
2591 if (SCARG(uap, count) == auio.uio_resid) {
2592 if (union_dircheckp) {
2593 error = union_dircheckp(td, &vp, fp);
2599 if ((vp->v_flag & VROOT) &&
2600 (vp->v_mount->mnt_flag & MNT_UNION)) {
2601 struct vnode *tvp = vp;
2602 vp = vp->v_mount->mnt_vnodecovered;
2604 fp->f_data = (caddr_t) vp;
2610 error = copyout((caddr_t)&loff, (caddr_t)SCARG(uap, basep),
2612 uap->sysmsg_result = SCARG(uap, count) - auio.uio_resid;
2615 #endif /* COMPAT_43 */
2618 * getdirentries_args(int fd, char *buf, u_int conut, long *basep)
2620 * Read a block of directory entries in a file system independent format.
2623 getdirentries(struct getdirentries_args *uap)
2625 struct thread *td = curthread;
2626 struct proc *p = td->td_proc;
2634 if ((error = getvnode(p->p_fd, SCARG(uap, fd), &fp)) != 0)
2636 if ((fp->f_flag & FREAD) == 0)
2638 vp = (struct vnode *)fp->f_data;
2640 if (vp->v_type != VDIR)
2642 aiov.iov_base = SCARG(uap, buf);
2643 aiov.iov_len = SCARG(uap, count);
2644 auio.uio_iov = &aiov;
2645 auio.uio_iovcnt = 1;
2646 auio.uio_rw = UIO_READ;
2647 auio.uio_segflg = UIO_USERSPACE;
2649 auio.uio_resid = SCARG(uap, count);
2650 /* vn_lock(vp, LK_SHARED | LK_RETRY, td); */
2651 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY, td);
2652 loff = auio.uio_offset = fp->f_offset;
2653 error = VOP_READDIR(vp, &auio, fp->f_cred, &eofflag, NULL, NULL);
2654 fp->f_offset = auio.uio_offset;
2655 VOP_UNLOCK(vp, 0, td);
2658 if (SCARG(uap, count) == auio.uio_resid) {
2659 if (union_dircheckp) {
2660 error = union_dircheckp(td, &vp, fp);
2666 if ((vp->v_flag & VROOT) &&
2667 (vp->v_mount->mnt_flag & MNT_UNION)) {
2668 struct vnode *tvp = vp;
2669 vp = vp->v_mount->mnt_vnodecovered;
2671 fp->f_data = (caddr_t) vp;
2677 if (SCARG(uap, basep) != NULL) {
2678 error = copyout((caddr_t)&loff, (caddr_t)SCARG(uap, basep),
2681 uap->sysmsg_result = SCARG(uap, count) - auio.uio_resid;
2686 * getdents_args(int fd, char *buf, size_t count)
2689 getdents(struct getdents_args *uap)
2691 struct getdirentries_args ap;
2695 ap.count = uap->count;
2697 return getdirentries(&ap);
2701 * umask(int newmask)
2703 * Set the mode mask for creation of filesystem nodes.
2708 umask(struct umask_args *uap)
2710 struct thread *td = curthread;
2711 struct proc *p = td->td_proc;
2712 struct filedesc *fdp;
2715 uap->sysmsg_result = fdp->fd_cmask;
2716 fdp->fd_cmask = SCARG(uap, newmask) & ALLPERMS;
2721 * revoke(char *path)
2723 * Void all references to file by ripping underlying filesystem
2728 revoke(struct revoke_args *uap)
2730 struct thread *td = curthread;
2731 struct proc *p = td->td_proc;
2735 struct nameidata nd;
2737 NDINIT(&nd, NAMEI_LOOKUP, CNP_FOLLOW, UIO_USERSPACE, SCARG(uap, path), td);
2738 if ((error = namei(&nd)) != 0)
2741 NDFREE(&nd, NDF_ONLY_PNBUF);
2742 if (vp->v_type != VCHR && vp->v_type != VBLK) {
2746 if ((error = VOP_GETATTR(vp, &vattr, td)) != 0)
2748 if (p->p_ucred->cr_uid != vattr.va_uid &&
2749 (error = suser_cred(p->p_ucred, PRISON_ROOT)))
2752 VOP_REVOKE(vp, REVOKEALL);
2759 * Convert a user file descriptor to a kernel file entry.
2762 getvnode(struct filedesc *fdp, int fd, struct file **fpp)
2766 if ((u_int)fd >= fdp->fd_nfiles ||
2767 (fp = fdp->fd_ofiles[fd]) == NULL)
2769 if (fp->f_type != DTYPE_VNODE && fp->f_type != DTYPE_FIFO)
2775 * getfh_args(char *fname, fhandle_t *fhp)
2777 * Get (NFS) file handle
2780 getfh(struct getfh_args *uap)
2782 struct thread *td = curthread;
2783 struct nameidata nd;
2789 * Must be super user
2794 NDINIT(&nd, NAMEI_LOOKUP, CNP_FOLLOW | CNP_LOCKLEAF, UIO_USERSPACE, uap->fname, td);
2798 NDFREE(&nd, NDF_ONLY_PNBUF);
2800 bzero(&fh, sizeof(fh));
2801 fh.fh_fsid = vp->v_mount->mnt_stat.f_fsid;
2802 error = VFS_VPTOFH(vp, &fh.fh_fid);
2806 error = copyout(&fh, uap->fhp, sizeof (fh));
2811 * fhopen_args(const struct fhandle *u_fhp, int flags)
2813 * syscall for the rpc.lockd to use to translate a NFS file handle into
2814 * an open descriptor.
2816 * warning: do not remove the suser() call or this becomes one giant
2820 fhopen(struct fhopen_args *uap)
2822 struct thread *td = curthread;
2823 struct proc *p = td->td_proc;
2828 struct vattr *vap = &vat;
2831 struct filedesc *fdp = p->p_fd;
2832 int fmode, mode, error, type;
2837 * Must be super user
2843 fmode = FFLAGS(SCARG(uap, flags));
2844 /* why not allow a non-read/write open for our lockd? */
2845 if (((fmode & (FREAD | FWRITE)) == 0) || (fmode & O_CREAT))
2847 error = copyin(SCARG(uap,u_fhp), &fhp, sizeof(fhp));
2850 /* find the mount point */
2851 mp = vfs_getvfs(&fhp.fh_fsid);
2854 /* now give me my vnode, it gets returned to me locked */
2855 error = VFS_FHTOVP(mp, &fhp.fh_fid, &vp);
2859 * from now on we have to make sure not
2860 * to forget about the vnode
2861 * any error that causes an abort must vput(vp)
2862 * just set error = err and 'goto bad;'.
2868 if (vp->v_type == VLNK) {
2872 if (vp->v_type == VSOCK) {
2877 if (fmode & (FWRITE | O_TRUNC)) {
2878 if (vp->v_type == VDIR) {
2882 error = vn_writechk(vp);
2890 error = VOP_ACCESS(vp, mode, p->p_ucred, td);
2894 if (fmode & O_TRUNC) {
2895 VOP_UNLOCK(vp, 0, td); /* XXX */
2896 VOP_LEASE(vp, td, p->p_ucred, LEASE_WRITE);
2897 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY, td); /* XXX */
2900 error = VOP_SETATTR(vp, vap, p->p_ucred, td);
2904 error = VOP_OPEN(vp, fmode, p->p_ucred, td);
2908 * Make sure that a VM object is created for VMIO support.
2910 if (vn_canvmio(vp) == TRUE) {
2911 if ((error = vfs_object_create(vp, td)) != 0)
2918 * end of vn_open code
2921 if ((error = falloc(p, &nfp, &indx)) != 0) {
2929 * hold an extra reference to avoid having fp ripped out
2930 * from under us while we block in the lock op.
2933 nfp->f_data = (caddr_t)vp;
2934 nfp->f_flag = fmode & FMASK;
2935 nfp->f_ops = &vnops;
2936 nfp->f_type = DTYPE_VNODE;
2937 if (fmode & (O_EXLOCK | O_SHLOCK)) {
2938 lf.l_whence = SEEK_SET;
2941 if (fmode & O_EXLOCK)
2942 lf.l_type = F_WRLCK;
2944 lf.l_type = F_RDLCK;
2946 if ((fmode & FNONBLOCK) == 0)
2948 VOP_UNLOCK(vp, 0, td);
2949 if ((error = VOP_ADVLOCK(vp, (caddr_t)fp, F_SETLK, &lf, type)) != 0) {
2951 * lock request failed. Normally close the descriptor
2952 * but handle the case where someone might have dup()d
2953 * or close()d it when we weren't looking.
2955 if (fdp->fd_ofiles[indx] == fp) {
2956 fdp->fd_ofiles[indx] = NULL;
2961 * release our private reference.
2966 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY, td);
2967 fp->f_flag |= FHASLOCK;
2969 if ((vp->v_type == VREG) && (VOP_GETVOBJECT(vp, NULL) != 0))
2970 vfs_object_create(vp, td);
2972 VOP_UNLOCK(vp, 0, td);
2974 uap->sysmsg_result = indx;
2983 * fhstat_args(struct fhandle *u_fhp, struct stat *sb)
2986 fhstat(struct fhstat_args *uap)
2988 struct thread *td = curthread;
2996 * Must be super user
3002 error = copyin(SCARG(uap, u_fhp), &fh, sizeof(fhandle_t));
3006 if ((mp = vfs_getvfs(&fh.fh_fsid)) == NULL)
3008 if ((error = VFS_FHTOVP(mp, &fh.fh_fid, &vp)))
3010 error = vn_stat(vp, &sb, td);
3014 error = copyout(&sb, SCARG(uap, sb), sizeof(sb));
3019 * fhstatfs_args(struct fhandle *u_fhp, struct statfs *buf)
3022 fhstatfs(struct fhstatfs_args *uap)
3024 struct thread *td = curthread;
3033 * Must be super user
3035 if ((error = suser(td)))
3038 if ((error = copyin(SCARG(uap, u_fhp), &fh, sizeof(fhandle_t))) != 0)
3041 if ((mp = vfs_getvfs(&fh.fh_fsid)) == NULL)
3043 if ((error = VFS_FHTOVP(mp, &fh.fh_fid, &vp)))
3048 if ((error = VFS_STATFS(mp, sp, td)) != 0)
3050 sp->f_flags = mp->mnt_flag & MNT_VISFLAGMASK;
3052 bcopy((caddr_t)sp, (caddr_t)&sb, sizeof(sb));
3053 sb.f_fsid.val[0] = sb.f_fsid.val[1] = 0;
3056 return (copyout(sp, SCARG(uap, buf), sizeof(*sp)));
3060 * Syscall to push extended attribute configuration information into the
3061 * VFS. Accepts a path, which it converts to a mountpoint, as well as
3062 * a command (int cmd), and attribute name and misc data. For now, the
3063 * attribute name is left in userspace for consumption by the VFS_op.
3064 * It will probably be changed to be copied into sysspace by the
3065 * syscall in the future, once issues with various consumers of the
3066 * attribute code have raised their hands.
3068 * Currently this is used only by UFS Extended Attributes.
3071 extattrctl(struct extattrctl_args *uap)
3073 struct thread *td = curthread;
3074 struct nameidata nd;
3078 NDINIT(&nd, NAMEI_LOOKUP, CNP_FOLLOW, UIO_USERSPACE, SCARG(uap, path), td);
3079 if ((error = namei(&nd)) != 0)
3081 mp = nd.ni_vp->v_mount;
3083 return (VFS_EXTATTRCTL(mp, SCARG(uap, cmd), SCARG(uap, attrname),
3084 SCARG(uap, arg), td));
3088 * Syscall to set a named extended attribute on a file or directory.
3089 * Accepts attribute name, and a uio structure pointing to the data to set.
3090 * The uio is consumed in the style of writev(). The real work happens
3091 * in VOP_SETEXTATTR().
3094 extattr_set_file(struct extattr_set_file_args *uap)
3096 struct thread *td = curthread;
3097 struct proc *p = td->td_proc;
3098 struct nameidata nd;
3100 struct iovec *iov, *needfree = NULL, aiov[UIO_SMALLIOV];
3101 char attrname[EXTATTR_MAXNAMELEN];
3105 error = copyin(SCARG(uap, attrname), attrname, EXTATTR_MAXNAMELEN);
3108 NDINIT(&nd, NAMEI_LOOKUP, CNP_FOLLOW | CNP_LOCKLEAF, UIO_USERSPACE,
3109 SCARG(uap, path), td);
3110 if ((error = namei(&nd)) != 0)
3112 iovlen = uap->iovcnt * sizeof(struct iovec);
3113 if (uap->iovcnt > UIO_SMALLIOV) {
3114 if (uap->iovcnt > UIO_MAXIOV) {
3118 MALLOC(iov, struct iovec *, iovlen, M_IOV, M_WAITOK);
3123 auio.uio_iovcnt = uap->iovcnt;
3124 auio.uio_rw = UIO_WRITE;
3125 auio.uio_segflg = UIO_USERSPACE;
3127 auio.uio_offset = 0;
3128 if ((error = copyin((caddr_t)uap->iovp, (caddr_t)iov, iovlen)))
3131 for (i = 0; i < uap->iovcnt; i++) {
3132 if (iov->iov_len > INT_MAX - auio.uio_resid) {
3136 auio.uio_resid += iov->iov_len;
3139 cnt = auio.uio_resid;
3140 error = VOP_SETEXTATTR(nd.ni_vp, attrname, &auio, p->p_ucred, td);
3141 cnt -= auio.uio_resid;
3142 uap->sysmsg_result = cnt;
3145 FREE(needfree, M_IOV);
3151 * Syscall to get a named extended attribute on a file or directory.
3152 * Accepts attribute name, and a uio structure pointing to a buffer for the
3153 * data. The uio is consumed in the style of readv(). The real work
3154 * happens in VOP_GETEXTATTR();
3157 extattr_get_file(struct extattr_get_file_args *uap)
3159 struct thread *td = curthread;
3160 struct proc *p = td->td_proc;
3161 struct nameidata nd;
3163 struct iovec *iov, *needfree, aiov[UIO_SMALLIOV];
3164 char attrname[EXTATTR_MAXNAMELEN];
3168 error = copyin(SCARG(uap, attrname), attrname, EXTATTR_MAXNAMELEN);
3171 NDINIT(&nd, NAMEI_LOOKUP, CNP_FOLLOW | CNP_LOCKLEAF, UIO_USERSPACE,
3172 SCARG(uap, path), td);
3173 if ((error = namei(&nd)) != 0)
3175 iovlen = uap->iovcnt * sizeof (struct iovec);
3176 if (uap->iovcnt > UIO_SMALLIOV) {
3177 if (uap->iovcnt > UIO_MAXIOV) {
3181 MALLOC(iov, struct iovec *, iovlen, M_IOV, M_WAITOK);
3188 auio.uio_iovcnt = uap->iovcnt;
3189 auio.uio_rw = UIO_READ;
3190 auio.uio_segflg = UIO_USERSPACE;
3192 auio.uio_offset = 0;
3193 if ((error = copyin((caddr_t)uap->iovp, (caddr_t)iov, iovlen)))
3196 for (i = 0; i < uap->iovcnt; i++) {
3197 if (iov->iov_len > INT_MAX - auio.uio_resid) {
3201 auio.uio_resid += iov->iov_len;
3204 cnt = auio.uio_resid;
3205 error = VOP_GETEXTATTR(nd.ni_vp, attrname, &auio, p->p_ucred, td);
3206 cnt -= auio.uio_resid;
3207 uap->sysmsg_result = cnt;
3210 FREE(needfree, M_IOV);
3216 * Syscall to delete a named extended attribute from a file or directory.
3217 * Accepts attribute name. The real work happens in VOP_SETEXTATTR().
3220 extattr_delete_file(struct extattr_delete_file_args *uap)
3222 struct thread *td = curthread;
3223 struct proc *p = td->td_proc;
3224 struct nameidata nd;
3225 char attrname[EXTATTR_MAXNAMELEN];
3228 error = copyin(SCARG(uap, attrname), attrname, EXTATTR_MAXNAMELEN);
3231 NDINIT(&nd, NAMEI_LOOKUP, CNP_FOLLOW | CNP_LOCKLEAF, UIO_USERSPACE,
3232 SCARG(uap, path), td);
3233 if ((error = namei(&nd)) != 0)
3235 error = VOP_SETEXTATTR(nd.ni_vp, attrname, NULL, p->p_ucred, td);