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.9 2003/07/19 21:14:39 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 <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 #include <sys/file2.h>
76 static int change_dir __P((struct nameidata *ndp, struct thread *td));
77 static void checkdirs __P((struct vnode *olddp));
78 static int chroot_refuse_vdir_fds __P((struct filedesc *fdp));
79 static int getutimes __P((const struct timeval *, struct timespec *));
80 static int setfown __P((struct vnode *, uid_t, gid_t));
81 static int setfmode __P((struct vnode *, int));
82 static int setfflags __P((struct vnode *, int));
83 static int setutimes __P((struct vnode *, const struct timespec *, int));
84 static int usermount = 0; /* if 1, non-root can mount fs. */
86 int (*union_dircheckp) __P((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.
97 #ifndef _SYS_SYSPROTO_H_
106 * mount_args(char *type, char *path, int flags, caddr_t data)
110 mount(struct mount_args *uap)
112 struct thread *td = curthread;
113 struct proc *p = td->td_proc;
116 struct vfsconf *vfsp;
117 int error, flag = 0, flag2 = 0;
123 char fstypename[MFSNAMELEN];
125 if (usermount == 0 && (error = suser(td)))
128 * Do not allow NFS export by non-root users.
130 if (SCARG(uap, flags) & MNT_EXPORTED) {
136 * Silently enforce MNT_NOSUID and MNT_NODEV for non-root users
139 SCARG(uap, flags) |= MNT_NOSUID | MNT_NODEV;
141 * Get vnode to be covered
143 NDINIT(&nd, LOOKUP, FOLLOW | LOCKLEAF, UIO_USERSPACE,
144 SCARG(uap, path), td);
145 if ((error = namei(&nd)) != 0)
147 NDFREE(&nd, NDF_ONLY_PNBUF);
149 if (SCARG(uap, flags) & MNT_UPDATE) {
150 if ((vp->v_flag & VROOT) == 0) {
156 flag2 = mp->mnt_kern_flag;
158 * We only allow the filesystem to be reloaded if it
159 * is currently mounted read-only.
161 if ((SCARG(uap, flags) & MNT_RELOAD) &&
162 ((mp->mnt_flag & MNT_RDONLY) == 0)) {
164 return (EOPNOTSUPP); /* Needs translation */
167 * Only root, or the user that did the original mount is
168 * permitted to update it.
170 if (mp->mnt_stat.f_owner != p->p_ucred->cr_uid &&
171 (error = suser(td))) {
175 if (vfs_busy(mp, LK_NOWAIT, 0, td)) {
179 lwkt_gettoken(&vp->v_interlock);
180 if ((vp->v_flag & VMOUNT) != 0 ||
181 vp->v_mountedhere != NULL) {
182 lwkt_reltoken(&vp->v_interlock);
187 vp->v_flag |= VMOUNT;
188 lwkt_reltoken(&vp->v_interlock);
190 SCARG(uap, flags) & (MNT_RELOAD | MNT_FORCE | MNT_UPDATE);
191 VOP_UNLOCK(vp, 0, td);
195 * If the user is not root, ensure that they own the directory
196 * onto which we are attempting to mount.
198 if ((error = VOP_GETATTR(vp, &va, td)) ||
199 (va.va_uid != p->p_ucred->cr_uid &&
200 (error = suser(td)))) {
204 if ((error = vinvalbuf(vp, V_SAVE, td, 0, 0)) != 0) {
208 if (vp->v_type != VDIR) {
214 * Historically filesystem types were identified by number. If we
215 * get an integer for the filesystem type instead of a string, we
216 * check to see if it matches one of the historic filesystem types.
218 fstypenum = (uintptr_t)SCARG(uap, type);
219 if (fstypenum < maxvfsconf) {
220 for (vfsp = vfsconf; vfsp; vfsp = vfsp->vfc_next)
221 if (vfsp->vfc_typenum == fstypenum)
227 strncpy(fstypename, vfsp->vfc_name, MFSNAMELEN);
229 #endif /* COMPAT_43 */
230 if ((error = copyinstr(SCARG(uap, type), fstypename, MFSNAMELEN, NULL)) != 0) {
234 for (vfsp = vfsconf; vfsp; vfsp = vfsp->vfc_next)
235 if (!strcmp(vfsp->vfc_name, fstypename))
240 /* Only load modules for root (very important!) */
241 if ((error = suser(td)) != 0) {
245 error = linker_load_file(fstypename, &lf);
246 if (error || lf == NULL) {
253 /* lookup again, see if the VFS was loaded */
254 for (vfsp = vfsconf; vfsp; vfsp = vfsp->vfc_next)
255 if (!strcmp(vfsp->vfc_name, fstypename))
259 linker_file_unload(lf);
264 lwkt_gettoken(&vp->v_interlock);
265 if ((vp->v_flag & VMOUNT) != 0 ||
266 vp->v_mountedhere != NULL) {
267 lwkt_reltoken(&vp->v_interlock);
271 vp->v_flag |= VMOUNT;
272 lwkt_reltoken(&vp->v_interlock);
275 * Allocate and initialize the filesystem.
277 mp = malloc(sizeof(struct mount), M_MOUNT, M_WAITOK);
278 bzero((char *)mp, (u_long)sizeof(struct mount));
279 TAILQ_INIT(&mp->mnt_nvnodelist);
280 TAILQ_INIT(&mp->mnt_reservedvnlist);
281 mp->mnt_nvnodelistsize = 0;
282 lockinit(&mp->mnt_lock, 0, "vfslock", 0, LK_NOPAUSE);
283 (void)vfs_busy(mp, LK_NOWAIT, 0, td);
284 mp->mnt_op = vfsp->vfc_vfsops;
286 vfsp->vfc_refcount++;
287 mp->mnt_stat.f_type = vfsp->vfc_typenum;
288 mp->mnt_flag |= vfsp->vfc_flags & MNT_VISFLAGMASK;
289 strncpy(mp->mnt_stat.f_fstypename, vfsp->vfc_name, MFSNAMELEN);
290 mp->mnt_vnodecovered = vp;
291 mp->mnt_stat.f_owner = p->p_ucred->cr_uid;
292 mp->mnt_iosize_max = DFLTPHYS;
293 VOP_UNLOCK(vp, 0, td);
296 * Set the mount level flags.
298 if (SCARG(uap, flags) & MNT_RDONLY)
299 mp->mnt_flag |= MNT_RDONLY;
300 else if (mp->mnt_flag & MNT_RDONLY)
301 mp->mnt_kern_flag |= MNTK_WANTRDWR;
302 mp->mnt_flag &=~ (MNT_NOSUID | MNT_NOEXEC | MNT_NODEV |
303 MNT_SYNCHRONOUS | MNT_UNION | MNT_ASYNC | MNT_NOATIME |
304 MNT_NOSYMFOLLOW | MNT_IGNORE |
305 MNT_NOCLUSTERR | MNT_NOCLUSTERW | MNT_SUIDDIR);
306 mp->mnt_flag |= SCARG(uap, flags) & (MNT_NOSUID | MNT_NOEXEC |
307 MNT_NODEV | MNT_SYNCHRONOUS | MNT_UNION | MNT_ASYNC | MNT_FORCE |
308 MNT_NOSYMFOLLOW | MNT_IGNORE |
309 MNT_NOATIME | MNT_NOCLUSTERR | MNT_NOCLUSTERW | MNT_SUIDDIR);
311 * Mount the filesystem.
312 * XXX The final recipients of VFS_MOUNT just overwrite the ndp they
313 * get. No freeing of cn_pnbuf.
315 error = VFS_MOUNT(mp, SCARG(uap, path), SCARG(uap, data), &nd, td);
316 if (mp->mnt_flag & MNT_UPDATE) {
317 if (mp->mnt_kern_flag & MNTK_WANTRDWR)
318 mp->mnt_flag &= ~MNT_RDONLY;
319 mp->mnt_flag &=~ (MNT_UPDATE | MNT_RELOAD | MNT_FORCE);
320 mp->mnt_kern_flag &=~ MNTK_WANTRDWR;
323 mp->mnt_kern_flag = flag2;
325 if ((mp->mnt_flag & MNT_RDONLY) == 0) {
326 if (mp->mnt_syncer == NULL)
327 error = vfs_allocate_syncvnode(mp);
329 if (mp->mnt_syncer != NULL)
330 vrele(mp->mnt_syncer);
331 mp->mnt_syncer = NULL;
334 lwkt_gettoken(&vp->v_interlock);
335 vp->v_flag &= ~VMOUNT;
336 lwkt_reltoken(&vp->v_interlock);
340 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY, td);
342 * Put the new filesystem on the mount list after root.
346 lwkt_gettoken(&vp->v_interlock);
347 vp->v_flag &= ~VMOUNT;
348 vp->v_mountedhere = mp;
349 lwkt_reltoken(&vp->v_interlock);
350 lwkt_gettoken(&mountlist_token);
351 TAILQ_INSERT_TAIL(&mountlist, mp, mnt_list);
352 lwkt_reltoken(&mountlist_token);
354 VOP_UNLOCK(vp, 0, td);
355 if ((mp->mnt_flag & MNT_RDONLY) == 0)
356 error = vfs_allocate_syncvnode(mp);
358 if ((error = VFS_START(mp, 0, td)) != 0)
361 lwkt_gettoken(&vp->v_interlock);
362 vp->v_flag &= ~VMOUNT;
363 lwkt_reltoken(&vp->v_interlock);
364 mp->mnt_vfc->vfc_refcount--;
366 free((caddr_t)mp, M_MOUNT);
373 * Scan all active processes to see if any of them have a current
374 * or root directory onto which the new filesystem has just been
375 * mounted. If so, replace them with the new mount point.
378 checkdirs(struct vnode *olddp)
380 struct filedesc *fdp;
384 if (olddp->v_usecount == 1)
386 if (VFS_ROOT(olddp->v_mountedhere, &newdp))
387 panic("mount: lost mount");
388 LIST_FOREACH(p, &allproc, p_list) {
390 if (fdp->fd_cdir == olddp) {
393 fdp->fd_cdir = newdp;
395 if (fdp->fd_rdir == olddp) {
398 fdp->fd_rdir = newdp;
401 if (rootvnode == olddp) {
410 * Unmount a file system.
412 * Note: unmount takes a path to the vnode mounted on as argument,
413 * not special file (as before).
415 #ifndef _SYS_SYSPROTO_H_
416 struct unmount_args {
422 * umount_args(char *path, int flags)
426 unmount(struct unmount_args *uap)
428 struct thread *td = curthread;
429 struct proc *p = td->td_proc;
436 NDINIT(&nd, LOOKUP, FOLLOW | LOCKLEAF, UIO_USERSPACE,
437 SCARG(uap, path), td);
438 if ((error = namei(&nd)) != 0)
441 NDFREE(&nd, NDF_ONLY_PNBUF);
445 * Only root, or the user that did the original mount is
446 * permitted to unmount this filesystem.
448 if ((mp->mnt_stat.f_owner != p->p_ucred->cr_uid) &&
449 (error = suser(td))) {
455 * Don't allow unmounting the root file system.
457 if (mp->mnt_flag & MNT_ROOTFS) {
463 * Must be the root of the filesystem
465 if ((vp->v_flag & VROOT) == 0) {
470 return (dounmount(mp, SCARG(uap, flags), td));
474 * Do the actual file system unmount.
477 dounmount(struct mount *mp, int flags, struct thread *td)
479 struct vnode *coveredvp;
483 lwkt_gettoken(&mountlist_token);
484 if (mp->mnt_kern_flag & MNTK_UNMOUNT) {
485 lwkt_reltoken(&mountlist_token);
488 mp->mnt_kern_flag |= MNTK_UNMOUNT;
489 /* Allow filesystems to detect that a forced unmount is in progress. */
490 if (flags & MNT_FORCE)
491 mp->mnt_kern_flag |= MNTK_UNMOUNTF;
492 error = lockmgr(&mp->mnt_lock, LK_DRAIN | LK_INTERLOCK |
493 ((flags & MNT_FORCE) ? 0 : LK_NOWAIT), &mountlist_token, td);
495 mp->mnt_kern_flag &= ~(MNTK_UNMOUNT | MNTK_UNMOUNTF);
496 if (mp->mnt_kern_flag & MNTK_MWAIT)
501 if (mp->mnt_flag & MNT_EXPUBLIC)
502 vfs_setpublicfs(NULL, NULL, NULL);
504 vfs_msync(mp, MNT_WAIT);
505 async_flag = mp->mnt_flag & MNT_ASYNC;
506 mp->mnt_flag &=~ MNT_ASYNC;
507 cache_purgevfs(mp); /* remove cache entries for this file sys */
508 if (mp->mnt_syncer != NULL)
509 vrele(mp->mnt_syncer);
510 if (((mp->mnt_flag & MNT_RDONLY) ||
511 (error = VFS_SYNC(mp, MNT_WAIT, td)) == 0) ||
513 error = VFS_UNMOUNT(mp, flags, td);
514 lwkt_gettoken(&mountlist_token);
516 if ((mp->mnt_flag & MNT_RDONLY) == 0 && mp->mnt_syncer == NULL)
517 (void) vfs_allocate_syncvnode(mp);
518 mp->mnt_kern_flag &= ~(MNTK_UNMOUNT | MNTK_UNMOUNTF);
519 mp->mnt_flag |= async_flag;
520 lockmgr(&mp->mnt_lock, LK_RELEASE | LK_INTERLOCK | LK_REENABLE,
521 &mountlist_token, td);
522 if (mp->mnt_kern_flag & MNTK_MWAIT)
526 TAILQ_REMOVE(&mountlist, mp, mnt_list);
527 if ((coveredvp = mp->mnt_vnodecovered) != NULLVP) {
528 coveredvp->v_mountedhere = (struct mount *)0;
531 mp->mnt_vfc->vfc_refcount--;
532 if (!TAILQ_EMPTY(&mp->mnt_nvnodelist))
533 panic("unmount: dangling vnode");
534 lockmgr(&mp->mnt_lock, LK_RELEASE | LK_INTERLOCK, &mountlist_token, td);
535 if (mp->mnt_kern_flag & MNTK_MWAIT)
537 free((caddr_t)mp, M_MOUNT);
542 * Sync each mounted filesystem.
544 #ifndef _SYS_SYSPROTO_H_
551 static int syncprt = 0;
552 SYSCTL_INT(_debug, OID_AUTO, syncprt, CTLFLAG_RW, &syncprt, 0, "");
557 sync(struct sync_args *uap)
559 struct thread *td = curthread;
560 struct mount *mp, *nmp;
563 lwkt_gettoken(&mountlist_token);
564 for (mp = TAILQ_FIRST(&mountlist); mp != NULL; mp = nmp) {
565 if (vfs_busy(mp, LK_NOWAIT, &mountlist_token, td)) {
566 nmp = TAILQ_NEXT(mp, mnt_list);
569 if ((mp->mnt_flag & MNT_RDONLY) == 0) {
570 asyncflag = mp->mnt_flag & MNT_ASYNC;
571 mp->mnt_flag &= ~MNT_ASYNC;
572 vfs_msync(mp, MNT_NOWAIT);
573 VFS_SYNC(mp, MNT_NOWAIT, td);
574 mp->mnt_flag |= asyncflag;
576 lwkt_gettoken(&mountlist_token);
577 nmp = TAILQ_NEXT(mp, mnt_list);
580 lwkt_reltoken(&mountlist_token);
583 * XXX don't call vfs_bufstats() yet because that routine
584 * was not imported in the Lite2 merge.
589 #endif /* DIAGNOSTIC */
594 /* XXX PRISON: could be per prison flag */
595 static int prison_quotas;
597 SYSCTL_INT(_kern_prison, OID_AUTO, quotas, CTLFLAG_RW, &prison_quotas, 0, "");
601 * quotactl_args(char *path, int fcmd, int uid, caddr_t arg)
603 * Change filesystem quotas.
607 quotactl(struct quotactl_args *uap)
609 struct thread *td = curthread;
610 struct proc *p = td->td_proc;
616 if (p->p_ucred->cr_prison && !prison_quotas)
618 NDINIT(&nd, LOOKUP, FOLLOW, UIO_USERSPACE, SCARG(uap, path), td);
619 if ((error = namei(&nd)) != 0)
621 mp = nd.ni_vp->v_mount;
622 NDFREE(&nd, NDF_ONLY_PNBUF);
624 return (VFS_QUOTACTL(mp, SCARG(uap, cmd), SCARG(uap, uid),
625 SCARG(uap, arg), td));
629 * statfs_args(char *path, struct statfs *buf)
631 * Get filesystem statistics.
635 statfs(struct statfs_args *uap)
637 struct thread *td = curthread;
644 NDINIT(&nd, LOOKUP, FOLLOW, UIO_USERSPACE, SCARG(uap, path), td);
645 if ((error = namei(&nd)) != 0)
647 mp = nd.ni_vp->v_mount;
649 NDFREE(&nd, NDF_ONLY_PNBUF);
651 error = VFS_STATFS(mp, sp, td);
654 sp->f_flags = mp->mnt_flag & MNT_VISFLAGMASK;
656 bcopy((caddr_t)sp, (caddr_t)&sb, sizeof(sb));
657 sb.f_fsid.val[0] = sb.f_fsid.val[1] = 0;
660 return (copyout((caddr_t)sp, (caddr_t)SCARG(uap, buf), sizeof(*sp)));
664 * fstatfs_args(int fd, struct statfs *buf)
666 * Get filesystem statistics.
670 fstatfs(struct fstatfs_args *uap)
672 struct thread *td = curthread;
673 struct proc *p = td->td_proc;
676 register struct statfs *sp;
681 if ((error = getvnode(p->p_fd, SCARG(uap, fd), &fp)) != 0)
683 mp = ((struct vnode *)fp->f_data)->v_mount;
687 error = VFS_STATFS(mp, sp, td);
690 sp->f_flags = mp->mnt_flag & MNT_VISFLAGMASK;
692 bcopy((caddr_t)sp, (caddr_t)&sb, sizeof(sb));
693 sb.f_fsid.val[0] = sb.f_fsid.val[1] = 0;
696 return (copyout((caddr_t)sp, (caddr_t)SCARG(uap, buf), sizeof(*sp)));
700 * getfsstat_args(struct statfs *buf, long bufsize, int flags)
702 * Get statistics on all filesystems.
706 getfsstat(struct getfsstat_args *uap)
708 struct thread *td = curthread;
709 struct proc *p = td->td_proc;
710 struct mount *mp, *nmp;
713 long count, maxcount, error;
715 maxcount = SCARG(uap, bufsize) / sizeof(struct statfs);
716 sfsp = (caddr_t)SCARG(uap, buf);
718 lwkt_gettoken(&mountlist_token);
719 for (mp = TAILQ_FIRST(&mountlist); mp != NULL; mp = nmp) {
720 if (vfs_busy(mp, LK_NOWAIT, &mountlist_token, td)) {
721 nmp = TAILQ_NEXT(mp, mnt_list);
724 if (sfsp && count < maxcount) {
727 * If MNT_NOWAIT or MNT_LAZY is specified, do not
728 * refresh the fsstat cache. MNT_NOWAIT or MNT_LAZY
729 * overrides MNT_WAIT.
731 if (((SCARG(uap, flags) & (MNT_LAZY|MNT_NOWAIT)) == 0 ||
732 (SCARG(uap, flags) & MNT_WAIT)) &&
733 (error = VFS_STATFS(mp, sp, td))) {
734 lwkt_gettoken(&mountlist_token);
735 nmp = TAILQ_NEXT(mp, mnt_list);
739 sp->f_flags = mp->mnt_flag & MNT_VISFLAGMASK;
740 error = copyout((caddr_t)sp, sfsp, sizeof(*sp));
748 lwkt_gettoken(&mountlist_token);
749 nmp = TAILQ_NEXT(mp, mnt_list);
752 lwkt_reltoken(&mountlist_token);
753 if (sfsp && count > maxcount)
754 p->p_retval[0] = maxcount;
756 p->p_retval[0] = count;
761 * fchdir_args(int fd)
763 * Change current working directory to a given file descriptor.
767 fchdir(struct fchdir_args *uap)
769 struct thread *td = curthread;
770 struct proc *p = td->td_proc;
771 struct filedesc *fdp = p->p_fd;
772 struct vnode *vp, *tdp;
777 if ((error = getvnode(fdp, SCARG(uap, fd), &fp)) != 0)
779 vp = (struct vnode *)fp->f_data;
781 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY, td);
782 if (vp->v_type != VDIR)
785 error = VOP_ACCESS(vp, VEXEC, p->p_ucred, td);
786 while (!error && (mp = vp->v_mountedhere) != NULL) {
787 if (vfs_busy(mp, 0, 0, td))
789 error = VFS_ROOT(mp, &tdp);
800 VOP_UNLOCK(vp, 0, td);
807 * chdir_args(char *path)
809 * Change current working directory (``.'').
813 chdir(struct chdir_args *uap)
815 struct thread *td = curthread;
816 struct proc *p = td->td_proc;
817 struct filedesc *fdp = p->p_fd;
821 NDINIT(&nd, LOOKUP, FOLLOW | LOCKLEAF, UIO_USERSPACE,
822 SCARG(uap, path), td);
823 if ((error = change_dir(&nd, td)) != 0)
825 NDFREE(&nd, NDF_ONLY_PNBUF);
827 fdp->fd_cdir = nd.ni_vp;
832 * Helper function for raised chroot(2) security function: Refuse if
833 * any filedescriptors are open directories.
836 chroot_refuse_vdir_fds(fdp)
837 struct filedesc *fdp;
844 for (fd = 0; fd < fdp->fd_nfiles ; fd++) {
845 error = getvnode(fdp, fd, &fp);
848 vp = (struct vnode *)fp->f_data;
849 if (vp->v_type != VDIR)
857 * This sysctl determines if we will allow a process to chroot(2) if it
858 * has a directory open:
859 * 0: disallowed for all processes.
860 * 1: allowed for processes that were not already chroot(2)'ed.
861 * 2: allowed for all processes.
864 static int chroot_allow_open_directories = 1;
866 SYSCTL_INT(_kern, OID_AUTO, chroot_allow_open_directories, CTLFLAG_RW,
867 &chroot_allow_open_directories, 0, "");
870 * chroot_args(char *path)
872 * Change notion of root (``/'') directory.
876 chroot(struct chroot_args *uap)
878 struct thread *td = curthread;
879 struct proc *p = td->td_proc;
880 struct filedesc *fdp = p->p_fd;
885 error = suser_cred(p->p_ucred, PRISON_ROOT);
888 if (chroot_allow_open_directories == 0 ||
889 (chroot_allow_open_directories == 1 && fdp->fd_rdir != rootvnode))
890 error = chroot_refuse_vdir_fds(fdp);
893 NDINIT(&nd, LOOKUP, FOLLOW | LOCKLEAF, UIO_USERSPACE,
894 SCARG(uap, path), td);
895 if ((error = change_dir(&nd, td)) != 0)
897 NDFREE(&nd, NDF_ONLY_PNBUF);
899 fdp->fd_rdir = nd.ni_vp;
901 fdp->fd_jdir = nd.ni_vp;
908 * Common routine for chroot and chdir.
911 change_dir(struct nameidata *ndp, struct thread *td)
920 if (vp->v_type != VDIR)
923 error = VOP_ACCESS(vp, VEXEC, ndp->ni_cnd.cn_cred, td);
927 VOP_UNLOCK(vp, 0, td);
932 * open_args(char *path, int flags, int mode)
934 * Check permissions, allocate an open file structure,
935 * and call the device open routine if any.
938 open(struct open_args *uap)
940 struct thread *td = curthread;
941 struct proc *p = td->td_proc;
942 struct filedesc *fdp = p->p_fd;
945 int cmode, flags, oflags;
947 int type, indx, error;
951 oflags = SCARG(uap, flags);
952 if ((oflags & O_ACCMODE) == O_ACCMODE)
954 flags = FFLAGS(oflags);
955 error = falloc(p, &nfp, &indx);
959 cmode = ((SCARG(uap, mode) &~ fdp->fd_cmask) & ALLPERMS) &~ S_ISTXT;
960 NDINIT(&nd, LOOKUP, FOLLOW, UIO_USERSPACE, SCARG(uap, path), td);
961 p->p_dupfd = -indx - 1; /* XXX check for fdopen */
963 * Bump the ref count to prevent another process from closing
964 * the descriptor while we are blocked in vn_open()
967 error = vn_open(&nd, flags, cmode);
970 * release our own reference
975 * handle special fdopen() case. bleh. dupfdopen() is
976 * responsible for dropping the old contents of ofiles[indx]
979 if ((error == ENODEV || error == ENXIO) &&
980 p->p_dupfd >= 0 && /* XXX from fdopen */
982 dupfdopen(fdp, indx, p->p_dupfd, flags, error)) == 0) {
983 p->p_retval[0] = indx;
987 * Clean up the descriptor, but only if another thread hadn't
988 * replaced or closed it.
990 if (fdp->fd_ofiles[indx] == fp) {
991 fdp->fd_ofiles[indx] = NULL;
995 if (error == ERESTART)
1000 NDFREE(&nd, NDF_ONLY_PNBUF);
1004 * There should be 2 references on the file, one from the descriptor
1005 * table, and one for us.
1007 * Handle the case where someone closed the file (via its file
1008 * descriptor) while we were blocked. The end result should look
1009 * like opening the file succeeded but it was immediately closed.
1011 if (fp->f_count == 1) {
1012 KASSERT(fdp->fd_ofiles[indx] != fp,
1013 ("Open file descriptor lost all refs"));
1014 VOP_UNLOCK(vp, 0, td);
1015 vn_close(vp, flags & FMASK, td);
1017 p->p_retval[0] = indx;
1021 fp->f_data = (caddr_t)vp;
1022 fp->f_flag = flags & FMASK;
1024 fp->f_type = (vp->v_type == VFIFO ? DTYPE_FIFO : DTYPE_VNODE);
1025 if (flags & (O_EXLOCK | O_SHLOCK)) {
1026 lf.l_whence = SEEK_SET;
1029 if (flags & O_EXLOCK)
1030 lf.l_type = F_WRLCK;
1032 lf.l_type = F_RDLCK;
1034 if ((flags & FNONBLOCK) == 0)
1036 VOP_UNLOCK(vp, 0, td);
1037 if ((error = VOP_ADVLOCK(vp, (caddr_t)fp, F_SETLK, &lf, type)) != 0) {
1039 * lock request failed. Normally close the descriptor
1040 * but handle the case where someone might have dup()d
1041 * it when we weren't looking. One reference is
1042 * owned by the descriptor array, the other by us.
1044 if (fdp->fd_ofiles[indx] == fp) {
1045 fdp->fd_ofiles[indx] = NULL;
1051 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY, td);
1052 fp->f_flag |= FHASLOCK;
1054 /* assert that vn_open created a backing object if one is needed */
1055 KASSERT(!vn_canvmio(vp) || VOP_GETVOBJECT(vp, NULL) == 0,
1056 ("open: vmio vnode has no backing object after vn_open"));
1057 VOP_UNLOCK(vp, 0, td);
1060 * release our private reference, leaving the one associated with the
1061 * descriptor table intact.
1064 p->p_retval[0] = indx;
1070 * ocreat(char *path, int mode)
1075 ocreat(struct ocreat_args *uap)
1077 struct open_args /* {
1078 syscallarg(char *) path;
1079 syscallarg(int) flags;
1080 syscallarg(int) mode;
1083 SCARG(&nuap, path) = SCARG(uap, path);
1084 SCARG(&nuap, mode) = SCARG(uap, mode);
1085 SCARG(&nuap, flags) = O_WRONLY | O_CREAT | O_TRUNC;
1086 return (open(&nuap));
1088 #endif /* COMPAT_43 */
1091 * mknod_args(char *path, int mode, int dev)
1093 * Create a special file.
1097 mknod(struct mknod_args *uap)
1099 struct thread *td = curthread;
1100 struct proc *p = td->td_proc;
1105 struct nameidata nd;
1109 switch (SCARG(uap, mode) & S_IFMT) {
1115 error = suser_cred(p->p_ucred, PRISON_ROOT);
1121 NDINIT(&nd, CREATE, LOCKPARENT, UIO_USERSPACE, SCARG(uap, path), td);
1122 if ((error = namei(&nd)) != 0)
1129 vattr.va_mode = (SCARG(uap, mode) & ALLPERMS) &~ p->p_fd->fd_cmask;
1130 vattr.va_rdev = SCARG(uap, dev);
1133 switch (SCARG(uap, mode) & S_IFMT) {
1134 case S_IFMT: /* used by badsect to flag bad sectors */
1135 vattr.va_type = VBAD;
1138 vattr.va_type = VCHR;
1141 vattr.va_type = VBLK;
1152 VOP_LEASE(nd.ni_dvp, td, p->p_ucred, LEASE_WRITE);
1154 error = VOP_WHITEOUT(nd.ni_dvp, &nd.ni_cnd, CREATE);
1156 error = VOP_MKNOD(nd.ni_dvp, &nd.ni_vp,
1157 &nd.ni_cnd, &vattr);
1161 NDFREE(&nd, NDF_ONLY_PNBUF);
1164 NDFREE(&nd, NDF_ONLY_PNBUF);
1165 if (nd.ni_dvp == vp)
1172 ASSERT_VOP_UNLOCKED(nd.ni_dvp, "mknod");
1173 ASSERT_VOP_UNLOCKED(nd.ni_vp, "mknod");
1178 * mkfifo_args(char *path, int mode)
1180 * Create a named pipe.
1184 mkfifo(struct mkfifo_args *uap)
1186 struct thread *td = curthread;
1187 struct proc *p = td->td_proc;
1190 struct nameidata nd;
1193 NDINIT(&nd, CREATE, LOCKPARENT, UIO_USERSPACE, SCARG(uap, path), td);
1194 if ((error = namei(&nd)) != 0)
1196 if (nd.ni_vp != NULL) {
1197 NDFREE(&nd, NDF_ONLY_PNBUF);
1198 if (nd.ni_dvp == nd.ni_vp)
1206 vattr.va_type = VFIFO;
1207 vattr.va_mode = (SCARG(uap, mode) & ALLPERMS) &~ p->p_fd->fd_cmask;
1208 VOP_LEASE(nd.ni_dvp, td, p->p_ucred, LEASE_WRITE);
1209 error = VOP_MKNOD(nd.ni_dvp, &nd.ni_vp, &nd.ni_cnd, &vattr);
1212 NDFREE(&nd, NDF_ONLY_PNBUF);
1218 * link_args(char *path, char *link)
1220 * Make a hard file link.
1224 link(struct link_args *uap)
1226 struct thread *td = curthread;
1227 struct proc *p = td->td_proc;
1229 struct nameidata nd;
1233 NDINIT(&nd, LOOKUP, FOLLOW|NOOBJ, UIO_USERSPACE, SCARG(uap, path), td);
1234 if ((error = namei(&nd)) != 0)
1236 NDFREE(&nd, NDF_ONLY_PNBUF);
1238 if (vp->v_type == VDIR)
1239 error = EPERM; /* POSIX */
1241 NDINIT(&nd, CREATE, LOCKPARENT|NOOBJ, UIO_USERSPACE, SCARG(uap, link), td);
1244 if (nd.ni_vp != NULL) {
1249 VOP_LEASE(nd.ni_dvp, td, p->p_ucred,
1251 VOP_LEASE(vp, td, p->p_ucred, LEASE_WRITE);
1252 error = VOP_LINK(nd.ni_dvp, vp, &nd.ni_cnd);
1254 NDFREE(&nd, NDF_ONLY_PNBUF);
1255 if (nd.ni_dvp == nd.ni_vp)
1262 ASSERT_VOP_UNLOCKED(nd.ni_dvp, "link");
1263 ASSERT_VOP_UNLOCKED(nd.ni_vp, "link");
1268 * symlink(char *path, char *link)
1270 * Make a symbolic link.
1274 symlink(struct symlink_args *uap)
1276 struct thread *td = curthread;
1277 struct proc *p = td->td_proc;
1281 struct nameidata nd;
1283 path = zalloc(namei_zone);
1284 if ((error = copyinstr(SCARG(uap, path), path, MAXPATHLEN, NULL)) != 0)
1287 NDINIT(&nd, CREATE, LOCKPARENT|NOOBJ, UIO_USERSPACE, SCARG(uap, link), td);
1288 if ((error = namei(&nd)) != 0)
1291 NDFREE(&nd, NDF_ONLY_PNBUF);
1292 if (nd.ni_dvp == nd.ni_vp)
1301 vattr.va_mode = ACCESSPERMS &~ p->p_fd->fd_cmask;
1302 VOP_LEASE(nd.ni_dvp, td, p->p_ucred, LEASE_WRITE);
1303 error = VOP_SYMLINK(nd.ni_dvp, &nd.ni_vp, &nd.ni_cnd, &vattr, path);
1304 NDFREE(&nd, NDF_ONLY_PNBUF);
1308 ASSERT_VOP_UNLOCKED(nd.ni_dvp, "symlink");
1309 ASSERT_VOP_UNLOCKED(nd.ni_vp, "symlink");
1311 zfree(namei_zone, path);
1316 * undelete_args(char *path)
1318 * Delete a whiteout from the filesystem.
1322 undelete(struct undelete_args *uap)
1324 struct thread *td = curthread;
1325 struct proc *p = td->td_proc;
1327 struct nameidata nd;
1330 NDINIT(&nd, DELETE, LOCKPARENT|DOWHITEOUT, UIO_USERSPACE,
1331 SCARG(uap, path), td);
1336 if (nd.ni_vp != NULLVP || !(nd.ni_cnd.cn_flags & ISWHITEOUT)) {
1337 NDFREE(&nd, NDF_ONLY_PNBUF);
1338 if (nd.ni_dvp == nd.ni_vp)
1347 VOP_LEASE(nd.ni_dvp, td, p->p_ucred, LEASE_WRITE);
1348 error = VOP_WHITEOUT(nd.ni_dvp, &nd.ni_cnd, DELETE);
1349 NDFREE(&nd, NDF_ONLY_PNBUF);
1351 ASSERT_VOP_UNLOCKED(nd.ni_dvp, "undelete");
1352 ASSERT_VOP_UNLOCKED(nd.ni_vp, "undelete");
1357 * unlink_args(char *path)
1359 * Delete a name from the filesystem.
1362 unlink(struct unlink_args *uap)
1364 struct thread *td = curthread;
1365 struct proc *p = td->td_proc;
1368 struct nameidata nd;
1371 NDINIT(&nd, DELETE, LOCKPARENT, UIO_USERSPACE, SCARG(uap, path), td);
1372 if ((error = namei(&nd)) != 0)
1375 VOP_LEASE(vp, td, p->p_ucred, LEASE_WRITE);
1376 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY, td);
1378 if (vp->v_type == VDIR)
1379 error = EPERM; /* POSIX */
1382 * The root of a mounted filesystem cannot be deleted.
1384 * XXX: can this only be a VDIR case?
1386 if (vp->v_flag & VROOT)
1391 VOP_LEASE(nd.ni_dvp, td, p->p_ucred, LEASE_WRITE);
1392 error = VOP_REMOVE(nd.ni_dvp, vp, &nd.ni_cnd);
1394 NDFREE(&nd, NDF_ONLY_PNBUF);
1395 if (nd.ni_dvp == vp)
1401 ASSERT_VOP_UNLOCKED(nd.ni_dvp, "unlink");
1402 ASSERT_VOP_UNLOCKED(nd.ni_vp, "unlink");
1407 * lseek_args(int fd, int pad, off_t offset, int whence)
1409 * Reposition read/write file offset.
1412 lseek(struct lseek_args *uap)
1414 struct thread *td = curthread;
1415 struct proc *p = td->td_proc;
1416 struct filedesc *fdp = p->p_fd;
1421 if ((u_int)SCARG(uap, fd) >= fdp->fd_nfiles ||
1422 (fp = fdp->fd_ofiles[SCARG(uap, fd)]) == NULL)
1424 if (fp->f_type != DTYPE_VNODE)
1426 switch (SCARG(uap, whence)) {
1428 fp->f_offset += SCARG(uap, offset);
1431 error=VOP_GETATTR((struct vnode *)fp->f_data, &vattr, td);
1434 fp->f_offset = SCARG(uap, offset) + vattr.va_size;
1437 fp->f_offset = SCARG(uap, offset);
1442 *(off_t *)(p->p_retval) = fp->f_offset;
1446 #if defined(COMPAT_43) || defined(COMPAT_SUNOS)
1448 * Reposition read/write file offset.
1450 * olseek_args(int fd, long offset, int whence)
1453 olseek(struct olseek_args *uap)
1455 struct lseek_args /* {
1457 syscallarg(int) pad;
1458 syscallarg(off_t) offset;
1459 syscallarg(int) whence;
1463 SCARG(&nuap, fd) = SCARG(uap, fd);
1464 SCARG(&nuap, offset) = SCARG(uap, offset);
1465 SCARG(&nuap, whence) = SCARG(uap, whence);
1466 error = lseek(&nuap);
1469 #endif /* COMPAT_43 */
1472 * access_args(char *path, int flags)
1474 * Check access permissions.
1477 access(struct access_args *uap)
1479 struct thread *td = curthread;
1480 struct proc *p = td->td_proc;
1481 struct ucred *cred, *tmpcred;
1484 struct nameidata nd;
1488 * Create and modify a temporary credential instead of one that
1489 * is potentially shared. This could also mess up socket
1490 * buffer accounting which can run in an interrupt context.
1492 tmpcred = crdup(cred);
1493 tmpcred->cr_uid = p->p_ucred->cr_ruid;
1494 tmpcred->cr_groups[0] = p->p_ucred->cr_rgid;
1495 p->p_ucred = tmpcred;
1496 NDINIT(&nd, LOOKUP, FOLLOW | LOCKLEAF | NOOBJ, UIO_USERSPACE,
1497 SCARG(uap, path), td);
1498 if ((error = namei(&nd)) != 0)
1502 /* Flags == 0 means only check for existence. */
1503 if (SCARG(uap, flags)) {
1505 if (SCARG(uap, flags) & R_OK)
1507 if (SCARG(uap, flags) & W_OK)
1509 if (SCARG(uap, flags) & X_OK)
1511 if ((flags & VWRITE) == 0 || (error = vn_writechk(vp)) == 0)
1512 error = VOP_ACCESS(vp, flags, tmpcred, td);
1514 NDFREE(&nd, NDF_ONLY_PNBUF);
1522 #if defined(COMPAT_43) || defined(COMPAT_SUNOS)
1524 * ostat_args(char *path, struct ostat *ub)
1526 * Get file status; this version follows links.
1530 ostat(struct ostat_args *uap)
1532 struct thread *td = curthread;
1536 struct nameidata nd;
1538 NDINIT(&nd, LOOKUP, FOLLOW | LOCKLEAF | NOOBJ, UIO_USERSPACE,
1539 SCARG(uap, path), td);
1540 if ((error = namei(&nd)) != 0)
1542 NDFREE(&nd, NDF_ONLY_PNBUF);
1543 error = vn_stat(nd.ni_vp, &sb, td);
1548 error = copyout((caddr_t)&osb, (caddr_t)SCARG(uap, ub), sizeof (osb));
1553 * olstat_args(char *path, struct ostat *ub)
1555 * Get file status; this version does not follow links.
1559 olstat(struct olstat_args *uap)
1561 struct thread *td = curthread;
1566 struct nameidata nd;
1568 NDINIT(&nd, LOOKUP, NOFOLLOW | LOCKLEAF | NOOBJ, UIO_USERSPACE,
1569 SCARG(uap, path), td);
1570 if ((error = namei(&nd)) != 0)
1573 error = vn_stat(vp, &sb, td);
1574 NDFREE(&nd, NDF_ONLY_PNBUF);
1579 error = copyout((caddr_t)&osb, (caddr_t)SCARG(uap, ub), sizeof (osb));
1584 * Convert from an old to a new stat structure.
1591 ost->st_dev = st->st_dev;
1592 ost->st_ino = st->st_ino;
1593 ost->st_mode = st->st_mode;
1594 ost->st_nlink = st->st_nlink;
1595 ost->st_uid = st->st_uid;
1596 ost->st_gid = st->st_gid;
1597 ost->st_rdev = st->st_rdev;
1598 if (st->st_size < (quad_t)1 << 32)
1599 ost->st_size = st->st_size;
1602 ost->st_atime = st->st_atime;
1603 ost->st_mtime = st->st_mtime;
1604 ost->st_ctime = st->st_ctime;
1605 ost->st_blksize = st->st_blksize;
1606 ost->st_blocks = st->st_blocks;
1607 ost->st_flags = st->st_flags;
1608 ost->st_gen = st->st_gen;
1610 #endif /* COMPAT_43 || COMPAT_SUNOS */
1613 * stat_args(char *path, struct stat *ub)
1615 * Get file status; this version follows links.
1619 stat(struct stat_args *uap)
1621 struct thread *td = curthread;
1624 struct nameidata nd;
1626 NDINIT(&nd, LOOKUP, FOLLOW | LOCKLEAF | NOOBJ, UIO_USERSPACE,
1627 SCARG(uap, path), td);
1628 if ((error = namei(&nd)) != 0)
1630 error = vn_stat(nd.ni_vp, &sb, td);
1631 NDFREE(&nd, NDF_ONLY_PNBUF);
1635 error = copyout((caddr_t)&sb, (caddr_t)SCARG(uap, ub), sizeof (sb));
1640 * lstat_args(char *path, struct stat *ub)
1642 * Get file status; this version does not follow links.
1646 lstat(struct lstat_args *uap)
1648 struct thread *td = curthread;
1652 struct nameidata nd;
1654 NDINIT(&nd, LOOKUP, NOFOLLOW | LOCKLEAF | NOOBJ, UIO_USERSPACE,
1655 SCARG(uap, path), td);
1656 if ((error = namei(&nd)) != 0)
1659 error = vn_stat(vp, &sb, td);
1660 NDFREE(&nd, NDF_ONLY_PNBUF);
1664 error = copyout((caddr_t)&sb, (caddr_t)SCARG(uap, ub), sizeof (sb));
1673 nsb->st_dev = sb->st_dev;
1674 nsb->st_ino = sb->st_ino;
1675 nsb->st_mode = sb->st_mode;
1676 nsb->st_nlink = sb->st_nlink;
1677 nsb->st_uid = sb->st_uid;
1678 nsb->st_gid = sb->st_gid;
1679 nsb->st_rdev = sb->st_rdev;
1680 nsb->st_atimespec = sb->st_atimespec;
1681 nsb->st_mtimespec = sb->st_mtimespec;
1682 nsb->st_ctimespec = sb->st_ctimespec;
1683 nsb->st_size = sb->st_size;
1684 nsb->st_blocks = sb->st_blocks;
1685 nsb->st_blksize = sb->st_blksize;
1686 nsb->st_flags = sb->st_flags;
1687 nsb->st_gen = sb->st_gen;
1688 nsb->st_qspare[0] = sb->st_qspare[0];
1689 nsb->st_qspare[1] = sb->st_qspare[1];
1693 * nstat_args(char *path, struct nstat *ub)
1697 nstat(struct nstat_args *uap)
1699 struct thread *td = curthread;
1703 struct nameidata nd;
1705 NDINIT(&nd, LOOKUP, FOLLOW | LOCKLEAF | NOOBJ, UIO_USERSPACE,
1706 SCARG(uap, path), td);
1707 if ((error = namei(&nd)) != 0)
1709 NDFREE(&nd, NDF_ONLY_PNBUF);
1710 error = vn_stat(nd.ni_vp, &sb, td);
1714 cvtnstat(&sb, &nsb);
1715 error = copyout((caddr_t)&nsb, (caddr_t)SCARG(uap, ub), sizeof (nsb));
1720 * lstat_args(char *path, struct stat *ub)
1722 * Get file status; this version does not follow links.
1726 nlstat(struct nlstat_args *uap)
1728 struct thread *td = curthread;
1733 struct nameidata nd;
1735 NDINIT(&nd, LOOKUP, NOFOLLOW | LOCKLEAF | NOOBJ, UIO_USERSPACE,
1736 SCARG(uap, path), td);
1737 if ((error = namei(&nd)) != 0)
1740 NDFREE(&nd, NDF_ONLY_PNBUF);
1741 error = vn_stat(vp, &sb, td);
1745 cvtnstat(&sb, &nsb);
1746 error = copyout((caddr_t)&nsb, (caddr_t)SCARG(uap, ub), sizeof (nsb));
1751 * pathconf_Args(char *path, int name)
1753 * Get configurable pathname variables.
1757 pathconf(struct pathconf_args *uap)
1759 struct thread *td = curthread;
1760 struct proc *p = td->td_proc;
1762 struct nameidata nd;
1764 NDINIT(&nd, LOOKUP, FOLLOW | LOCKLEAF | NOOBJ, UIO_USERSPACE,
1765 SCARG(uap, path), td);
1766 if ((error = namei(&nd)) != 0)
1768 NDFREE(&nd, NDF_ONLY_PNBUF);
1769 error = VOP_PATHCONF(nd.ni_vp, SCARG(uap, name), p->p_retval);
1775 * readlink_args(char *path, char *buf, int count)
1777 * Return target name of a symbolic link.
1781 readlink(struct readlink_args *uap)
1783 struct thread *td = curthread;
1784 struct proc *p = td->td_proc;
1789 struct nameidata nd;
1791 NDINIT(&nd, LOOKUP, NOFOLLOW | LOCKLEAF | NOOBJ, UIO_USERSPACE,
1792 SCARG(uap, path), td);
1793 if ((error = namei(&nd)) != 0)
1795 NDFREE(&nd, NDF_ONLY_PNBUF);
1797 if (vp->v_type != VLNK)
1800 aiov.iov_base = SCARG(uap, buf);
1801 aiov.iov_len = SCARG(uap, count);
1802 auio.uio_iov = &aiov;
1803 auio.uio_iovcnt = 1;
1804 auio.uio_offset = 0;
1805 auio.uio_rw = UIO_READ;
1806 auio.uio_segflg = UIO_USERSPACE;
1808 auio.uio_resid = SCARG(uap, count);
1809 error = VOP_READLINK(vp, &auio, p->p_ucred);
1812 p->p_retval[0] = SCARG(uap, count) - auio.uio_resid;
1817 setfflags(struct vnode *vp, int flags)
1819 struct thread *td = curthread;
1820 struct proc *p = td->td_proc;
1825 * Prevent non-root users from setting flags on devices. When
1826 * a device is reused, users can retain ownership of the device
1827 * if they are allowed to set flags and programs assume that
1828 * chown can't fail when done as root.
1830 if ((vp->v_type == VCHR || vp->v_type == VBLK) &&
1831 ((error = suser_cred(p->p_ucred, PRISON_ROOT)) != 0))
1834 VOP_LEASE(vp, td, p->p_ucred, LEASE_WRITE);
1835 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY, td);
1837 vattr.va_flags = flags;
1838 error = VOP_SETATTR(vp, &vattr, p->p_ucred, td);
1839 VOP_UNLOCK(vp, 0, td);
1844 * chflags(char *path, int flags)
1846 * Change flags of a file given a path name.
1850 chflags(struct chflags_args *uap)
1852 struct thread *td = curthread;
1854 struct nameidata nd;
1856 NDINIT(&nd, LOOKUP, FOLLOW, UIO_USERSPACE, SCARG(uap, path), td);
1857 if ((error = namei(&nd)) != 0)
1859 NDFREE(&nd, NDF_ONLY_PNBUF);
1860 error = setfflags(nd.ni_vp, SCARG(uap, flags));
1866 * fchflags_args(int fd, int flags)
1868 * Change flags of a file given a file descriptor.
1872 fchflags(struct fchflags_args *uap)
1874 struct thread *td = curthread;
1875 struct proc *p = td->td_proc;
1879 if ((error = getvnode(p->p_fd, SCARG(uap, fd), &fp)) != 0)
1881 return setfflags((struct vnode *) fp->f_data, SCARG(uap, flags));
1885 setfmode(struct vnode *vp, int mode)
1887 struct thread *td = curthread;
1888 struct proc *p = td->td_proc;
1892 VOP_LEASE(vp, td, p->p_ucred, LEASE_WRITE);
1893 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY, td);
1895 vattr.va_mode = mode & ALLPERMS;
1896 error = VOP_SETATTR(vp, &vattr, p->p_ucred, td);
1897 VOP_UNLOCK(vp, 0, td);
1902 * chmod_args(char *path, int mode)
1904 * Change mode of a file given path name.
1908 chmod(struct chmod_args *uap)
1910 struct thread *td = curthread;
1912 struct nameidata nd;
1914 NDINIT(&nd, LOOKUP, FOLLOW, UIO_USERSPACE, SCARG(uap, path), td);
1915 if ((error = namei(&nd)) != 0)
1917 NDFREE(&nd, NDF_ONLY_PNBUF);
1918 error = setfmode(nd.ni_vp, SCARG(uap, mode));
1924 * lchmod_args(char *path, int mode)
1926 * Change mode of a file given path name (don't follow links.)
1930 lchmod(struct lchmod_args *uap)
1932 struct thread *td = curthread;
1934 struct nameidata nd;
1936 NDINIT(&nd, LOOKUP, NOFOLLOW, UIO_USERSPACE, SCARG(uap, path), td);
1937 if ((error = namei(&nd)) != 0)
1939 NDFREE(&nd, NDF_ONLY_PNBUF);
1940 error = setfmode(nd.ni_vp, SCARG(uap, mode));
1946 * fchmod_args(int fd, int mode)
1948 * Change mode of a file given a file descriptor.
1952 fchmod(struct fchmod_args *uap)
1954 struct thread *td = curthread;
1955 struct proc *p = td->td_proc;
1959 if ((error = getvnode(p->p_fd, SCARG(uap, fd), &fp)) != 0)
1961 return setfmode((struct vnode *)fp->f_data, SCARG(uap, mode));
1965 setfown(struct vnode *vp, uid_t uid, gid_t gid)
1967 struct thread *td = curthread;
1968 struct proc *p = td->td_proc;
1972 VOP_LEASE(vp, td, p->p_ucred, LEASE_WRITE);
1973 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY, td);
1977 error = VOP_SETATTR(vp, &vattr, p->p_ucred, td);
1978 VOP_UNLOCK(vp, 0, td);
1983 * chown(char *path, int uid, int gid)
1985 * Set ownership given a path name.
1989 chown(struct chown_args *uap)
1991 struct thread *td = curthread;
1993 struct nameidata nd;
1995 NDINIT(&nd, LOOKUP, FOLLOW, UIO_USERSPACE, SCARG(uap, path), td);
1996 if ((error = namei(&nd)) != 0)
1998 NDFREE(&nd, NDF_ONLY_PNBUF);
1999 error = setfown(nd.ni_vp, SCARG(uap, uid), SCARG(uap, gid));
2005 * lchown_args(char *path, int uid, int gid)
2007 * Set ownership given a path name, do not cross symlinks.
2011 lchown(struct lchown_args *uap)
2013 struct thread *td = curthread;
2015 struct nameidata nd;
2017 NDINIT(&nd, LOOKUP, NOFOLLOW, UIO_USERSPACE, SCARG(uap, path), td);
2018 if ((error = namei(&nd)) != 0)
2020 NDFREE(&nd, NDF_ONLY_PNBUF);
2021 error = setfown(nd.ni_vp, SCARG(uap, uid), SCARG(uap, gid));
2027 * fchown_args(int fd, int uid, int gid)
2029 * Set ownership given a file descriptor.
2033 fchown(struct fchown_args *uap)
2035 struct thread *td = curthread;
2036 struct proc *p = td->td_proc;
2040 if ((error = getvnode(p->p_fd, SCARG(uap, fd), &fp)) != 0)
2042 return setfown((struct vnode *)fp->f_data,
2043 SCARG(uap, uid), SCARG(uap, gid));
2047 getutimes(const struct timeval *usrtvp, struct timespec *tsp)
2049 struct timeval tv[2];
2052 if (usrtvp == NULL) {
2054 TIMEVAL_TO_TIMESPEC(&tv[0], &tsp[0]);
2057 if ((error = copyin(usrtvp, tv, sizeof (tv))) != 0)
2059 TIMEVAL_TO_TIMESPEC(&tv[0], &tsp[0]);
2060 TIMEVAL_TO_TIMESPEC(&tv[1], &tsp[1]);
2066 setutimes(struct vnode *vp, const struct timespec *ts, int nullflag)
2068 struct thread *td = curthread;
2069 struct proc *p = td->td_proc;
2073 VOP_LEASE(vp, td, p->p_ucred, LEASE_WRITE);
2074 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY, td);
2076 vattr.va_atime = ts[0];
2077 vattr.va_mtime = ts[1];
2079 vattr.va_vaflags |= VA_UTIMES_NULL;
2080 error = VOP_SETATTR(vp, &vattr, p->p_ucred, td);
2081 VOP_UNLOCK(vp, 0, td);
2086 * utimes_args(char *path, struct timeval *tptr)
2088 * Set the access and modification times of a file.
2092 utimes(struct utimes_args *uap)
2094 struct thread *td = curthread;
2095 struct timespec ts[2];
2096 struct timeval *usrtvp;
2098 struct nameidata nd;
2100 usrtvp = SCARG(uap, tptr);
2101 if ((error = getutimes(usrtvp, ts)) != 0)
2103 NDINIT(&nd, LOOKUP, FOLLOW, UIO_USERSPACE, SCARG(uap, path), td);
2104 if ((error = namei(&nd)) != 0)
2106 NDFREE(&nd, NDF_ONLY_PNBUF);
2107 error = setutimes(nd.ni_vp, ts, usrtvp == NULL);
2113 * lutimes_args(char *path, struct timeval *tptr)
2115 * Set the access and modification times of a file.
2119 lutimes(struct lutimes_args *uap)
2121 struct thread *td = curthread;
2122 struct timespec ts[2];
2123 struct timeval *usrtvp;
2125 struct nameidata nd;
2127 usrtvp = SCARG(uap, tptr);
2128 if ((error = getutimes(usrtvp, ts)) != 0)
2130 NDINIT(&nd, LOOKUP, NOFOLLOW, UIO_USERSPACE, SCARG(uap, path), td);
2131 if ((error = namei(&nd)) != 0)
2133 NDFREE(&nd, NDF_ONLY_PNBUF);
2134 error = setutimes(nd.ni_vp, ts, usrtvp == NULL);
2140 * futimes_args(int fd, struct timeval *tptr)
2142 * Set the access and modification times of a file.
2146 futimes(struct futimes_args *uap)
2148 struct thread *td = curthread;
2149 struct proc *p = td->td_proc;
2150 struct timespec ts[2];
2152 struct timeval *usrtvp;
2155 usrtvp = SCARG(uap, tptr);
2156 if ((error = getutimes(usrtvp, ts)) != 0)
2158 if ((error = getvnode(p->p_fd, SCARG(uap, fd), &fp)) != 0)
2160 return setutimes((struct vnode *)fp->f_data, ts, usrtvp == NULL);
2164 * truncate(char *path, int pad, off_t length)
2166 * Truncate a file given its path name.
2170 truncate(struct truncate_args *uap)
2172 struct thread *td = curthread;
2173 struct proc *p = td->td_proc;
2177 struct nameidata nd;
2179 if (uap->length < 0)
2181 NDINIT(&nd, LOOKUP, FOLLOW, UIO_USERSPACE, SCARG(uap, path), td);
2182 if ((error = namei(&nd)) != 0)
2185 NDFREE(&nd, NDF_ONLY_PNBUF);
2186 VOP_LEASE(vp, td, p->p_ucred, LEASE_WRITE);
2187 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY, td);
2188 if (vp->v_type == VDIR)
2190 else if ((error = vn_writechk(vp)) == 0 &&
2191 (error = VOP_ACCESS(vp, VWRITE, p->p_ucred, td)) == 0) {
2193 vattr.va_size = SCARG(uap, length);
2194 error = VOP_SETATTR(vp, &vattr, p->p_ucred, td);
2201 * ftruncate_args(int fd, int pad, off_t length)
2203 * Truncate a file given a file descriptor.
2207 ftruncate(struct ftruncate_args *uap)
2209 struct thread *td = curthread;
2210 struct proc *p = td->td_proc;
2216 if (uap->length < 0)
2218 if ((error = getvnode(p->p_fd, SCARG(uap, fd), &fp)) != 0)
2220 if ((fp->f_flag & FWRITE) == 0)
2222 vp = (struct vnode *)fp->f_data;
2223 VOP_LEASE(vp, td, p->p_ucred, LEASE_WRITE);
2224 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY, td);
2225 if (vp->v_type == VDIR)
2227 else if ((error = vn_writechk(vp)) == 0) {
2229 vattr.va_size = SCARG(uap, length);
2230 error = VOP_SETATTR(vp, &vattr, fp->f_cred, td);
2232 VOP_UNLOCK(vp, 0, td);
2236 #if defined(COMPAT_43) || defined(COMPAT_SUNOS)
2238 * otruncate_args(char *path, long length)
2240 * Truncate a file given its path name.
2244 otruncate(struct otruncate_args *uap)
2246 struct truncate_args /* {
2247 syscallarg(char *) path;
2248 syscallarg(int) pad;
2249 syscallarg(off_t) length;
2252 SCARG(&nuap, path) = SCARG(uap, path);
2253 SCARG(&nuap, length) = SCARG(uap, length);
2254 return (truncate(&nuap));
2258 * oftruncate_args(int fd, long length)
2260 * Truncate a file given a file descriptor.
2264 oftruncate(struct oftruncate_args *uap)
2266 struct ftruncate_args /* {
2268 syscallarg(int) pad;
2269 syscallarg(off_t) length;
2272 SCARG(&nuap, fd) = SCARG(uap, fd);
2273 SCARG(&nuap, length) = SCARG(uap, length);
2274 return (ftruncate(&nuap));
2276 #endif /* COMPAT_43 || COMPAT_SUNOS */
2281 * Sync an open file.
2285 fsync(struct fsync_args *uap)
2287 struct thread *td = curthread;
2288 struct proc *p = td->td_proc;
2294 if ((error = getvnode(p->p_fd, SCARG(uap, fd), &fp)) != 0)
2296 vp = (struct vnode *)fp->f_data;
2297 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY, td);
2298 if (VOP_GETVOBJECT(vp, &obj) == 0)
2299 vm_object_page_clean(obj, 0, 0, 0);
2300 if ((error = VOP_FSYNC(vp, MNT_WAIT, td)) == 0 &&
2301 vp->v_mount && (vp->v_mount->mnt_flag & MNT_SOFTDEP) &&
2303 error = (*bioops.io_fsync)(vp);
2304 VOP_UNLOCK(vp, 0, td);
2309 * rename_args(char *from, char *to)
2311 * Rename files. Source and destination must either both be directories,
2312 * or both not be directories. If target is a directory, it must be empty.
2316 rename(struct rename_args *uap)
2318 struct thread *td = curthread;
2319 struct proc *p = td->td_proc;
2320 struct vnode *tvp, *fvp, *tdvp;
2321 struct nameidata fromnd, tond;
2325 NDINIT(&fromnd, DELETE, WANTPARENT | SAVESTART, UIO_USERSPACE,
2326 SCARG(uap, from), td);
2327 if ((error = namei(&fromnd)) != 0)
2330 NDINIT(&tond, RENAME, LOCKPARENT | LOCKLEAF | NOCACHE | SAVESTART | NOOBJ,
2331 UIO_USERSPACE, SCARG(uap, to), td);
2332 if (fromnd.ni_vp->v_type == VDIR)
2333 tond.ni_cnd.cn_flags |= WILLBEDIR;
2334 if ((error = namei(&tond)) != 0) {
2335 /* Translate error code for rename("dir1", "dir2/."). */
2336 if (error == EISDIR && fvp->v_type == VDIR)
2338 NDFREE(&fromnd, NDF_ONLY_PNBUF);
2339 vrele(fromnd.ni_dvp);
2346 if (fvp->v_type == VDIR && tvp->v_type != VDIR) {
2349 } else if (fvp->v_type != VDIR && tvp->v_type == VDIR) {
2357 * If the source is the same as the destination (that is, if they
2358 * are links to the same vnode), then there is nothing to do.
2364 VOP_LEASE(tdvp, td, p->p_ucred, LEASE_WRITE);
2365 if (fromnd.ni_dvp != tdvp) {
2366 VOP_LEASE(fromnd.ni_dvp, td, p->p_ucred, LEASE_WRITE);
2369 VOP_LEASE(tvp, td, p->p_ucred, LEASE_WRITE);
2371 error = VOP_RENAME(fromnd.ni_dvp, fromnd.ni_vp, &fromnd.ni_cnd,
2372 tond.ni_dvp, tond.ni_vp, &tond.ni_cnd);
2373 NDFREE(&fromnd, NDF_ONLY_PNBUF);
2374 NDFREE(&tond, NDF_ONLY_PNBUF);
2376 NDFREE(&fromnd, NDF_ONLY_PNBUF);
2377 NDFREE(&tond, NDF_ONLY_PNBUF);
2384 vrele(fromnd.ni_dvp);
2387 vrele(tond.ni_startdir);
2388 ASSERT_VOP_UNLOCKED(fromnd.ni_dvp, "rename");
2389 ASSERT_VOP_UNLOCKED(fromnd.ni_vp, "rename");
2390 ASSERT_VOP_UNLOCKED(tond.ni_dvp, "rename");
2391 ASSERT_VOP_UNLOCKED(tond.ni_vp, "rename");
2393 if (fromnd.ni_startdir)
2394 vrele(fromnd.ni_startdir);
2401 * mkdir_args(char *path, int mode)
2403 * Make a directory file.
2407 mkdir(struct mkdir_args *uap)
2409 struct thread *td = curthread;
2410 struct proc *p = td->td_proc;
2414 struct nameidata nd;
2417 NDINIT(&nd, CREATE, LOCKPARENT, UIO_USERSPACE, SCARG(uap, path), td);
2418 nd.ni_cnd.cn_flags |= WILLBEDIR;
2419 if ((error = namei(&nd)) != 0)
2423 NDFREE(&nd, NDF_ONLY_PNBUF);
2424 if (nd.ni_dvp == vp)
2432 vattr.va_type = VDIR;
2433 vattr.va_mode = (SCARG(uap, mode) & ACCESSPERMS) &~ p->p_fd->fd_cmask;
2434 VOP_LEASE(nd.ni_dvp, td, p->p_ucred, LEASE_WRITE);
2435 error = VOP_MKDIR(nd.ni_dvp, &nd.ni_vp, &nd.ni_cnd, &vattr);
2436 NDFREE(&nd, NDF_ONLY_PNBUF);
2440 ASSERT_VOP_UNLOCKED(nd.ni_dvp, "mkdir");
2441 ASSERT_VOP_UNLOCKED(nd.ni_vp, "mkdir");
2446 * rmdir_args(char *path)
2448 * Remove a directory file.
2452 rmdir(struct rmdir_args *uap)
2454 struct thread *td = curthread;
2455 struct proc *p = td->td_proc;
2458 struct nameidata nd;
2461 NDINIT(&nd, DELETE, LOCKPARENT | LOCKLEAF, UIO_USERSPACE,
2462 SCARG(uap, path), td);
2463 if ((error = namei(&nd)) != 0)
2466 if (vp->v_type != VDIR) {
2471 * No rmdir "." please.
2473 if (nd.ni_dvp == vp) {
2478 * The root of a mounted filesystem cannot be deleted.
2480 if (vp->v_flag & VROOT)
2483 VOP_LEASE(nd.ni_dvp, td, p->p_ucred, LEASE_WRITE);
2484 VOP_LEASE(vp, td, p->p_ucred, LEASE_WRITE);
2485 error = VOP_RMDIR(nd.ni_dvp, nd.ni_vp, &nd.ni_cnd);
2488 NDFREE(&nd, NDF_ONLY_PNBUF);
2489 if (nd.ni_dvp == vp)
2495 ASSERT_VOP_UNLOCKED(nd.ni_dvp, "rmdir");
2496 ASSERT_VOP_UNLOCKED(nd.ni_vp, "rmdir");
2502 * ogetdirentries_args(int fd, char *buf, u_int count, long *basep)
2504 * Read a block of directory entries in a file system independent format.
2507 ogetdirentries(struct ogetdirentries_args *uap)
2509 struct thread *td = curthread;
2510 struct proc *p = td->td_proc;
2513 struct uio auio, kuio;
2514 struct iovec aiov, kiov;
2515 struct dirent *dp, *edp;
2517 int error, eofflag, readcnt;
2520 /* XXX arbitrary sanity limit on `count'. */
2521 if (SCARG(uap, count) > 64 * 1024)
2523 if ((error = getvnode(p->p_fd, SCARG(uap, fd), &fp)) != 0)
2525 if ((fp->f_flag & FREAD) == 0)
2527 vp = (struct vnode *)fp->f_data;
2529 if (vp->v_type != VDIR)
2531 aiov.iov_base = SCARG(uap, buf);
2532 aiov.iov_len = SCARG(uap, count);
2533 auio.uio_iov = &aiov;
2534 auio.uio_iovcnt = 1;
2535 auio.uio_rw = UIO_READ;
2536 auio.uio_segflg = UIO_USERSPACE;
2538 auio.uio_resid = SCARG(uap, count);
2539 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY, td);
2540 loff = auio.uio_offset = fp->f_offset;
2541 # if (BYTE_ORDER != LITTLE_ENDIAN)
2542 if (vp->v_mount->mnt_maxsymlinklen <= 0) {
2543 error = VOP_READDIR(vp, &auio, fp->f_cred, &eofflag,
2545 fp->f_offset = auio.uio_offset;
2550 kuio.uio_iov = &kiov;
2551 kuio.uio_segflg = UIO_SYSSPACE;
2552 kiov.iov_len = SCARG(uap, count);
2553 MALLOC(dirbuf, caddr_t, SCARG(uap, count), M_TEMP, M_WAITOK);
2554 kiov.iov_base = dirbuf;
2555 error = VOP_READDIR(vp, &kuio, fp->f_cred, &eofflag,
2557 fp->f_offset = kuio.uio_offset;
2559 readcnt = SCARG(uap, count) - kuio.uio_resid;
2560 edp = (struct dirent *)&dirbuf[readcnt];
2561 for (dp = (struct dirent *)dirbuf; dp < edp; ) {
2562 # if (BYTE_ORDER == LITTLE_ENDIAN)
2564 * The expected low byte of
2565 * dp->d_namlen is our dp->d_type.
2566 * The high MBZ byte of dp->d_namlen
2567 * is our dp->d_namlen.
2569 dp->d_type = dp->d_namlen;
2573 * The dp->d_type is the high byte
2574 * of the expected dp->d_namlen,
2575 * so must be zero'ed.
2579 if (dp->d_reclen > 0) {
2580 dp = (struct dirent *)
2581 ((char *)dp + dp->d_reclen);
2588 error = uiomove(dirbuf, readcnt, &auio);
2590 FREE(dirbuf, M_TEMP);
2592 VOP_UNLOCK(vp, 0, td);
2595 if (SCARG(uap, count) == auio.uio_resid) {
2596 if (union_dircheckp) {
2597 error = union_dircheckp(td, &vp, fp);
2603 if ((vp->v_flag & VROOT) &&
2604 (vp->v_mount->mnt_flag & MNT_UNION)) {
2605 struct vnode *tvp = vp;
2606 vp = vp->v_mount->mnt_vnodecovered;
2608 fp->f_data = (caddr_t) vp;
2614 error = copyout((caddr_t)&loff, (caddr_t)SCARG(uap, basep),
2616 p->p_retval[0] = SCARG(uap, count) - auio.uio_resid;
2619 #endif /* COMPAT_43 */
2622 * getdirentries_args(int fd, char *buf, u_int conut, long *basep)
2624 * Read a block of directory entries in a file system independent format.
2627 getdirentries(struct getdirentries_args *uap)
2629 struct thread *td = curthread;
2630 struct proc *p = td->td_proc;
2638 if ((error = getvnode(p->p_fd, SCARG(uap, fd), &fp)) != 0)
2640 if ((fp->f_flag & FREAD) == 0)
2642 vp = (struct vnode *)fp->f_data;
2644 if (vp->v_type != VDIR)
2646 aiov.iov_base = SCARG(uap, buf);
2647 aiov.iov_len = SCARG(uap, count);
2648 auio.uio_iov = &aiov;
2649 auio.uio_iovcnt = 1;
2650 auio.uio_rw = UIO_READ;
2651 auio.uio_segflg = UIO_USERSPACE;
2653 auio.uio_resid = SCARG(uap, count);
2654 /* vn_lock(vp, LK_SHARED | LK_RETRY, td); */
2655 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY, td);
2656 loff = auio.uio_offset = fp->f_offset;
2657 error = VOP_READDIR(vp, &auio, fp->f_cred, &eofflag, NULL, NULL);
2658 fp->f_offset = auio.uio_offset;
2659 VOP_UNLOCK(vp, 0, td);
2662 if (SCARG(uap, count) == auio.uio_resid) {
2663 if (union_dircheckp) {
2664 error = union_dircheckp(td, &vp, fp);
2670 if ((vp->v_flag & VROOT) &&
2671 (vp->v_mount->mnt_flag & MNT_UNION)) {
2672 struct vnode *tvp = vp;
2673 vp = vp->v_mount->mnt_vnodecovered;
2675 fp->f_data = (caddr_t) vp;
2681 if (SCARG(uap, basep) != NULL) {
2682 error = copyout((caddr_t)&loff, (caddr_t)SCARG(uap, basep),
2685 p->p_retval[0] = SCARG(uap, count) - auio.uio_resid;
2690 * getdents_args(int fd, char *buf, size_t count)
2693 getdents(struct getdents_args *uap)
2695 struct getdirentries_args ap;
2699 ap.count = uap->count;
2701 return getdirentries(&ap);
2705 * umask(int newmask)
2707 * Set the mode mask for creation of filesystem nodes.
2712 umask(struct umask_args *uap)
2714 struct thread *td = curthread;
2715 struct proc *p = td->td_proc;
2716 struct filedesc *fdp;
2719 p->p_retval[0] = fdp->fd_cmask;
2720 fdp->fd_cmask = SCARG(uap, newmask) & ALLPERMS;
2725 * revoke(char *path)
2727 * Void all references to file by ripping underlying filesystem
2732 revoke(struct revoke_args *uap)
2734 struct thread *td = curthread;
2735 struct proc *p = td->td_proc;
2739 struct nameidata nd;
2741 NDINIT(&nd, LOOKUP, FOLLOW, UIO_USERSPACE, SCARG(uap, path), td);
2742 if ((error = namei(&nd)) != 0)
2745 NDFREE(&nd, NDF_ONLY_PNBUF);
2746 if (vp->v_type != VCHR && vp->v_type != VBLK) {
2750 if ((error = VOP_GETATTR(vp, &vattr, td)) != 0)
2752 if (p->p_ucred->cr_uid != vattr.va_uid &&
2753 (error = suser_cred(p->p_ucred, PRISON_ROOT)))
2756 VOP_REVOKE(vp, REVOKEALL);
2763 * Convert a user file descriptor to a kernel file entry.
2766 getvnode(struct filedesc *fdp, int fd, struct file **fpp)
2770 if ((u_int)fd >= fdp->fd_nfiles ||
2771 (fp = fdp->fd_ofiles[fd]) == NULL)
2773 if (fp->f_type != DTYPE_VNODE && fp->f_type != DTYPE_FIFO)
2779 * getfh_args(char *fname, fhandle_t *fhp)
2781 * Get (NFS) file handle
2784 getfh(struct getfh_args *uap)
2786 struct thread *td = curthread;
2787 struct nameidata nd;
2793 * Must be super user
2798 NDINIT(&nd, LOOKUP, FOLLOW | LOCKLEAF, UIO_USERSPACE, uap->fname, td);
2802 NDFREE(&nd, NDF_ONLY_PNBUF);
2804 bzero(&fh, sizeof(fh));
2805 fh.fh_fsid = vp->v_mount->mnt_stat.f_fsid;
2806 error = VFS_VPTOFH(vp, &fh.fh_fid);
2810 error = copyout(&fh, uap->fhp, sizeof (fh));
2815 * fhopen_args(const struct fhandle *u_fhp, int flags)
2817 * syscall for the rpc.lockd to use to translate a NFS file handle into
2818 * an open descriptor.
2820 * warning: do not remove the suser() call or this becomes one giant
2824 fhopen(struct fhopen_args *uap)
2826 struct thread *td = curthread;
2827 struct proc *p = td->td_proc;
2832 struct vattr *vap = &vat;
2835 struct filedesc *fdp = p->p_fd;
2836 int fmode, mode, error, type;
2841 * Must be super user
2847 fmode = FFLAGS(SCARG(uap, flags));
2848 /* why not allow a non-read/write open for our lockd? */
2849 if (((fmode & (FREAD | FWRITE)) == 0) || (fmode & O_CREAT))
2851 error = copyin(SCARG(uap,u_fhp), &fhp, sizeof(fhp));
2854 /* find the mount point */
2855 mp = vfs_getvfs(&fhp.fh_fsid);
2858 /* now give me my vnode, it gets returned to me locked */
2859 error = VFS_FHTOVP(mp, &fhp.fh_fid, &vp);
2863 * from now on we have to make sure not
2864 * to forget about the vnode
2865 * any error that causes an abort must vput(vp)
2866 * just set error = err and 'goto bad;'.
2872 if (vp->v_type == VLNK) {
2876 if (vp->v_type == VSOCK) {
2881 if (fmode & (FWRITE | O_TRUNC)) {
2882 if (vp->v_type == VDIR) {
2886 error = vn_writechk(vp);
2894 error = VOP_ACCESS(vp, mode, p->p_ucred, td);
2898 if (fmode & O_TRUNC) {
2899 VOP_UNLOCK(vp, 0, td); /* XXX */
2900 VOP_LEASE(vp, td, p->p_ucred, LEASE_WRITE);
2901 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY, td); /* XXX */
2904 error = VOP_SETATTR(vp, vap, p->p_ucred, td);
2908 error = VOP_OPEN(vp, fmode, p->p_ucred, td);
2912 * Make sure that a VM object is created for VMIO support.
2914 if (vn_canvmio(vp) == TRUE) {
2915 if ((error = vfs_object_create(vp, td)) != 0)
2922 * end of vn_open code
2925 if ((error = falloc(p, &nfp, &indx)) != 0) {
2933 * hold an extra reference to avoid having fp ripped out
2934 * from under us while we block in the lock op.
2937 nfp->f_data = (caddr_t)vp;
2938 nfp->f_flag = fmode & FMASK;
2939 nfp->f_ops = &vnops;
2940 nfp->f_type = DTYPE_VNODE;
2941 if (fmode & (O_EXLOCK | O_SHLOCK)) {
2942 lf.l_whence = SEEK_SET;
2945 if (fmode & O_EXLOCK)
2946 lf.l_type = F_WRLCK;
2948 lf.l_type = F_RDLCK;
2950 if ((fmode & FNONBLOCK) == 0)
2952 VOP_UNLOCK(vp, 0, td);
2953 if ((error = VOP_ADVLOCK(vp, (caddr_t)fp, F_SETLK, &lf, type)) != 0) {
2955 * lock request failed. Normally close the descriptor
2956 * but handle the case where someone might have dup()d
2957 * or close()d it when we weren't looking.
2959 if (fdp->fd_ofiles[indx] == fp) {
2960 fdp->fd_ofiles[indx] = NULL;
2965 * release our private reference.
2970 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY, td);
2971 fp->f_flag |= FHASLOCK;
2973 if ((vp->v_type == VREG) && (VOP_GETVOBJECT(vp, NULL) != 0))
2974 vfs_object_create(vp, td);
2976 VOP_UNLOCK(vp, 0, td);
2978 p->p_retval[0] = indx;
2987 * fhstat_args(struct fhandle *u_fhp, struct stat *sb)
2990 fhstat(struct fhstat_args *uap)
2992 struct thread *td = curthread;
3000 * Must be super user
3006 error = copyin(SCARG(uap, u_fhp), &fh, sizeof(fhandle_t));
3010 if ((mp = vfs_getvfs(&fh.fh_fsid)) == NULL)
3012 if ((error = VFS_FHTOVP(mp, &fh.fh_fid, &vp)))
3014 error = vn_stat(vp, &sb, td);
3018 error = copyout(&sb, SCARG(uap, sb), sizeof(sb));
3023 * fhstatfs_args(struct fhandle *u_fhp, struct statfs *buf)
3026 fhstatfs(struct fhstatfs_args *uap)
3028 struct thread *td = curthread;
3037 * Must be super user
3039 if ((error = suser(td)))
3042 if ((error = copyin(SCARG(uap, u_fhp), &fh, sizeof(fhandle_t))) != 0)
3045 if ((mp = vfs_getvfs(&fh.fh_fsid)) == NULL)
3047 if ((error = VFS_FHTOVP(mp, &fh.fh_fid, &vp)))
3052 if ((error = VFS_STATFS(mp, sp, td)) != 0)
3054 sp->f_flags = mp->mnt_flag & MNT_VISFLAGMASK;
3056 bcopy((caddr_t)sp, (caddr_t)&sb, sizeof(sb));
3057 sb.f_fsid.val[0] = sb.f_fsid.val[1] = 0;
3060 return (copyout(sp, SCARG(uap, buf), sizeof(*sp)));
3064 * Syscall to push extended attribute configuration information into the
3065 * VFS. Accepts a path, which it converts to a mountpoint, as well as
3066 * a command (int cmd), and attribute name and misc data. For now, the
3067 * attribute name is left in userspace for consumption by the VFS_op.
3068 * It will probably be changed to be copied into sysspace by the
3069 * syscall in the future, once issues with various consumers of the
3070 * attribute code have raised their hands.
3072 * Currently this is used only by UFS Extended Attributes.
3075 extattrctl(struct extattrctl_args *uap)
3077 struct thread *td = curthread;
3078 struct nameidata nd;
3082 NDINIT(&nd, LOOKUP, FOLLOW, UIO_USERSPACE, SCARG(uap, path), td);
3083 if ((error = namei(&nd)) != 0)
3085 mp = nd.ni_vp->v_mount;
3087 return (VFS_EXTATTRCTL(mp, SCARG(uap, cmd), SCARG(uap, attrname),
3088 SCARG(uap, arg), td));
3092 * Syscall to set a named extended attribute on a file or directory.
3093 * Accepts attribute name, and a uio structure pointing to the data to set.
3094 * The uio is consumed in the style of writev(). The real work happens
3095 * in VOP_SETEXTATTR().
3098 extattr_set_file(struct extattr_set_file_args *uap)
3100 struct thread *td = curthread;
3101 struct proc *p = td->td_proc;
3102 struct nameidata nd;
3104 struct iovec *iov, *needfree = NULL, aiov[UIO_SMALLIOV];
3105 char attrname[EXTATTR_MAXNAMELEN];
3109 error = copyin(SCARG(uap, attrname), attrname, EXTATTR_MAXNAMELEN);
3112 NDINIT(&nd, LOOKUP, FOLLOW | LOCKLEAF, UIO_USERSPACE,
3113 SCARG(uap, path), td);
3114 if ((error = namei(&nd)) != 0)
3116 iovlen = uap->iovcnt * sizeof(struct iovec);
3117 if (uap->iovcnt > UIO_SMALLIOV) {
3118 if (uap->iovcnt > UIO_MAXIOV) {
3122 MALLOC(iov, struct iovec *, iovlen, M_IOV, M_WAITOK);
3127 auio.uio_iovcnt = uap->iovcnt;
3128 auio.uio_rw = UIO_WRITE;
3129 auio.uio_segflg = UIO_USERSPACE;
3131 auio.uio_offset = 0;
3132 if ((error = copyin((caddr_t)uap->iovp, (caddr_t)iov, iovlen)))
3135 for (i = 0; i < uap->iovcnt; i++) {
3136 if (iov->iov_len > INT_MAX - auio.uio_resid) {
3140 auio.uio_resid += iov->iov_len;
3143 cnt = auio.uio_resid;
3144 error = VOP_SETEXTATTR(nd.ni_vp, attrname, &auio, p->p_ucred, td);
3145 cnt -= auio.uio_resid;
3146 p->p_retval[0] = cnt;
3149 FREE(needfree, M_IOV);
3155 * Syscall to get a named extended attribute on a file or directory.
3156 * Accepts attribute name, and a uio structure pointing to a buffer for the
3157 * data. The uio is consumed in the style of readv(). The real work
3158 * happens in VOP_GETEXTATTR();
3161 extattr_get_file(struct extattr_get_file_args *uap)
3163 struct thread *td = curthread;
3164 struct proc *p = td->td_proc;
3165 struct nameidata nd;
3167 struct iovec *iov, *needfree, aiov[UIO_SMALLIOV];
3168 char attrname[EXTATTR_MAXNAMELEN];
3172 error = copyin(SCARG(uap, attrname), attrname, EXTATTR_MAXNAMELEN);
3175 NDINIT(&nd, LOOKUP, FOLLOW | LOCKLEAF, UIO_USERSPACE,
3176 SCARG(uap, path), td);
3177 if ((error = namei(&nd)) != 0)
3179 iovlen = uap->iovcnt * sizeof (struct iovec);
3180 if (uap->iovcnt > UIO_SMALLIOV) {
3181 if (uap->iovcnt > UIO_MAXIOV) {
3185 MALLOC(iov, struct iovec *, iovlen, M_IOV, M_WAITOK);
3192 auio.uio_iovcnt = uap->iovcnt;
3193 auio.uio_rw = UIO_READ;
3194 auio.uio_segflg = UIO_USERSPACE;
3196 auio.uio_offset = 0;
3197 if ((error = copyin((caddr_t)uap->iovp, (caddr_t)iov, iovlen)))
3200 for (i = 0; i < uap->iovcnt; i++) {
3201 if (iov->iov_len > INT_MAX - auio.uio_resid) {
3205 auio.uio_resid += iov->iov_len;
3208 cnt = auio.uio_resid;
3209 error = VOP_GETEXTATTR(nd.ni_vp, attrname, &auio, p->p_ucred, td);
3210 cnt -= auio.uio_resid;
3211 p->p_retval[0] = cnt;
3214 FREE(needfree, M_IOV);
3220 * Syscall to delete a named extended attribute from a file or directory.
3221 * Accepts attribute name. The real work happens in VOP_SETEXTATTR().
3224 extattr_delete_file(struct extattr_delete_file_args *uap)
3226 struct thread *td = curthread;
3227 struct proc *p = td->td_proc;
3228 struct nameidata nd;
3229 char attrname[EXTATTR_MAXNAMELEN];
3232 error = copyin(SCARG(uap, attrname), attrname, EXTATTR_MAXNAMELEN);
3235 NDINIT(&nd, LOOKUP, FOLLOW | LOCKLEAF, UIO_USERSPACE,
3236 SCARG(uap, path), td);
3237 if ((error = namei(&nd)) != 0)
3239 error = VOP_SETEXTATTR(nd.ni_vp, attrname, NULL, p->p_ucred, td);