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.6 2003/06/26 05:55:14 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 simple_lock(&vp->v_interlock);
180 if ((vp->v_flag & VMOUNT) != 0 ||
181 vp->v_mountedhere != NULL) {
182 simple_unlock(&vp->v_interlock);
187 vp->v_flag |= VMOUNT;
188 simple_unlock(&vp->v_interlock);
190 SCARG(uap, flags) & (MNT_RELOAD | MNT_FORCE | MNT_UPDATE);
191 VOP_UNLOCK(vp, 0, 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 simple_lock(&vp->v_interlock);
265 if ((vp->v_flag & VMOUNT) != 0 ||
266 vp->v_mountedhere != NULL) {
267 simple_unlock(&vp->v_interlock);
271 vp->v_flag |= VMOUNT;
272 simple_unlock(&vp->v_interlock);
275 * Allocate and initialize the filesystem.
277 mp = malloc(sizeof(struct mount), M_MOUNT, M_WAITOK);
278 bzero((char *)mp, (u_long)sizeof(struct mount));
279 TAILQ_INIT(&mp->mnt_nvnodelist);
280 TAILQ_INIT(&mp->mnt_reservedvnlist);
281 mp->mnt_nvnodelistsize = 0;
282 lockinit(&mp->mnt_lock, PVFS, "vfslock", 0, LK_NOPAUSE);
283 (void)vfs_busy(mp, LK_NOWAIT, 0, 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 simple_lock(&vp->v_interlock);
335 vp->v_flag &= ~VMOUNT;
336 simple_unlock(&vp->v_interlock);
340 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY, td);
342 * Put the new filesystem on the mount list after root.
346 simple_lock(&vp->v_interlock);
347 vp->v_flag &= ~VMOUNT;
348 vp->v_mountedhere = mp;
349 simple_unlock(&vp->v_interlock);
350 simple_lock(&mountlist_slock);
351 TAILQ_INSERT_TAIL(&mountlist, mp, mnt_list);
352 simple_unlock(&mountlist_slock);
354 VOP_UNLOCK(vp, 0, 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 simple_lock(&vp->v_interlock);
362 vp->v_flag &= ~VMOUNT;
363 simple_unlock(&vp->v_interlock);
364 mp->mnt_vfc->vfc_refcount--;
366 free((caddr_t)mp, M_MOUNT);
373 * Scan all active processes to see if any of them have a current
374 * or root directory onto which the new filesystem has just been
375 * mounted. If so, replace them with the new mount point.
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;
482 struct proc *p = td->td_proc;
486 simple_lock(&mountlist_slock);
487 if (mp->mnt_kern_flag & MNTK_UNMOUNT) {
488 simple_unlock(&mountlist_slock);
491 mp->mnt_kern_flag |= MNTK_UNMOUNT;
492 /* Allow filesystems to detect that a forced unmount is in progress. */
493 if (flags & MNT_FORCE)
494 mp->mnt_kern_flag |= MNTK_UNMOUNTF;
495 error = lockmgr(&mp->mnt_lock, LK_DRAIN | LK_INTERLOCK |
496 ((flags & MNT_FORCE) ? 0 : LK_NOWAIT), &mountlist_slock, td);
498 mp->mnt_kern_flag &= ~(MNTK_UNMOUNT | MNTK_UNMOUNTF);
499 if (mp->mnt_kern_flag & MNTK_MWAIT)
504 if (mp->mnt_flag & MNT_EXPUBLIC)
505 vfs_setpublicfs(NULL, NULL, NULL);
507 vfs_msync(mp, MNT_WAIT);
508 async_flag = mp->mnt_flag & MNT_ASYNC;
509 mp->mnt_flag &=~ MNT_ASYNC;
510 cache_purgevfs(mp); /* remove cache entries for this file sys */
511 if (mp->mnt_syncer != NULL)
512 vrele(mp->mnt_syncer);
513 if (((mp->mnt_flag & MNT_RDONLY) ||
514 (error = VFS_SYNC(mp, MNT_WAIT, td)) == 0) ||
516 error = VFS_UNMOUNT(mp, flags, td);
517 simple_lock(&mountlist_slock);
519 if ((mp->mnt_flag & MNT_RDONLY) == 0 && mp->mnt_syncer == NULL)
520 (void) vfs_allocate_syncvnode(mp);
521 mp->mnt_kern_flag &= ~(MNTK_UNMOUNT | MNTK_UNMOUNTF);
522 mp->mnt_flag |= async_flag;
523 lockmgr(&mp->mnt_lock, LK_RELEASE | LK_INTERLOCK | LK_REENABLE,
524 &mountlist_slock, td);
525 if (mp->mnt_kern_flag & MNTK_MWAIT)
529 TAILQ_REMOVE(&mountlist, mp, mnt_list);
530 if ((coveredvp = mp->mnt_vnodecovered) != NULLVP) {
531 coveredvp->v_mountedhere = (struct mount *)0;
534 mp->mnt_vfc->vfc_refcount--;
535 if (!TAILQ_EMPTY(&mp->mnt_nvnodelist))
536 panic("unmount: dangling vnode");
537 lockmgr(&mp->mnt_lock, LK_RELEASE | LK_INTERLOCK, &mountlist_slock, td);
538 if (mp->mnt_kern_flag & MNTK_MWAIT)
540 free((caddr_t)mp, M_MOUNT);
545 * Sync each mounted filesystem.
547 #ifndef _SYS_SYSPROTO_H_
554 static int syncprt = 0;
555 SYSCTL_INT(_debug, OID_AUTO, syncprt, CTLFLAG_RW, &syncprt, 0, "");
560 sync(struct sync_args *uap)
562 struct thread *td = curthread;
563 struct mount *mp, *nmp;
566 simple_lock(&mountlist_slock);
567 for (mp = TAILQ_FIRST(&mountlist); mp != NULL; mp = nmp) {
568 if (vfs_busy(mp, LK_NOWAIT, &mountlist_slock, td)) {
569 nmp = TAILQ_NEXT(mp, mnt_list);
572 if ((mp->mnt_flag & MNT_RDONLY) == 0) {
573 asyncflag = mp->mnt_flag & MNT_ASYNC;
574 mp->mnt_flag &= ~MNT_ASYNC;
575 vfs_msync(mp, MNT_NOWAIT);
576 VFS_SYNC(mp, MNT_NOWAIT, td);
577 mp->mnt_flag |= asyncflag;
579 simple_lock(&mountlist_slock);
580 nmp = TAILQ_NEXT(mp, mnt_list);
583 simple_unlock(&mountlist_slock);
586 * XXX don't call vfs_bufstats() yet because that routine
587 * was not imported in the Lite2 merge.
592 #endif /* DIAGNOSTIC */
597 /* XXX PRISON: could be per prison flag */
598 static int prison_quotas;
600 SYSCTL_INT(_kern_prison, OID_AUTO, quotas, CTLFLAG_RW, &prison_quotas, 0, "");
604 * quotactl_args(char *path, int fcmd, int uid, caddr_t arg)
606 * Change filesystem quotas.
610 quotactl(struct quotactl_args *uap)
612 struct thread *td = curthread;
613 struct proc *p = td->td_proc;
619 if (p->p_ucred->cr_prison && !prison_quotas)
621 NDINIT(&nd, LOOKUP, FOLLOW, UIO_USERSPACE, SCARG(uap, path), td);
622 if ((error = namei(&nd)) != 0)
624 mp = nd.ni_vp->v_mount;
625 NDFREE(&nd, NDF_ONLY_PNBUF);
627 return (VFS_QUOTACTL(mp, SCARG(uap, cmd), SCARG(uap, uid),
628 SCARG(uap, arg), td));
632 * statfs_args(char *path, struct statfs *buf)
634 * Get filesystem statistics.
638 statfs(struct statfs_args *uap)
640 struct thread *td = curthread;
647 NDINIT(&nd, LOOKUP, FOLLOW, UIO_USERSPACE, SCARG(uap, path), td);
648 if ((error = namei(&nd)) != 0)
650 mp = nd.ni_vp->v_mount;
652 NDFREE(&nd, NDF_ONLY_PNBUF);
654 error = VFS_STATFS(mp, sp, td);
657 sp->f_flags = mp->mnt_flag & MNT_VISFLAGMASK;
659 bcopy((caddr_t)sp, (caddr_t)&sb, sizeof(sb));
660 sb.f_fsid.val[0] = sb.f_fsid.val[1] = 0;
663 return (copyout((caddr_t)sp, (caddr_t)SCARG(uap, buf), sizeof(*sp)));
667 * fstatfs_args(int fd, struct statfs *buf)
669 * Get filesystem statistics.
673 fstatfs(struct fstatfs_args *uap)
675 struct thread *td = curthread;
676 struct proc *p = td->td_proc;
679 register struct statfs *sp;
684 if ((error = getvnode(p->p_fd, SCARG(uap, fd), &fp)) != 0)
686 mp = ((struct vnode *)fp->f_data)->v_mount;
690 error = VFS_STATFS(mp, sp, td);
693 sp->f_flags = mp->mnt_flag & MNT_VISFLAGMASK;
695 bcopy((caddr_t)sp, (caddr_t)&sb, sizeof(sb));
696 sb.f_fsid.val[0] = sb.f_fsid.val[1] = 0;
699 return (copyout((caddr_t)sp, (caddr_t)SCARG(uap, buf), sizeof(*sp)));
703 * getfsstat_args(struct statfs *buf, long bufsize, int flags)
705 * Get statistics on all filesystems.
709 getfsstat(struct getfsstat_args *uap)
711 struct thread *td = curthread;
712 struct proc *p = td->td_proc;
713 struct mount *mp, *nmp;
716 long count, maxcount, error;
718 maxcount = SCARG(uap, bufsize) / sizeof(struct statfs);
719 sfsp = (caddr_t)SCARG(uap, buf);
721 simple_lock(&mountlist_slock);
722 for (mp = TAILQ_FIRST(&mountlist); mp != NULL; mp = nmp) {
723 if (vfs_busy(mp, LK_NOWAIT, &mountlist_slock, td)) {
724 nmp = TAILQ_NEXT(mp, mnt_list);
727 if (sfsp && count < maxcount) {
730 * If MNT_NOWAIT or MNT_LAZY is specified, do not
731 * refresh the fsstat cache. MNT_NOWAIT or MNT_LAZY
732 * overrides MNT_WAIT.
734 if (((SCARG(uap, flags) & (MNT_LAZY|MNT_NOWAIT)) == 0 ||
735 (SCARG(uap, flags) & MNT_WAIT)) &&
736 (error = VFS_STATFS(mp, sp, td))) {
737 simple_lock(&mountlist_slock);
738 nmp = TAILQ_NEXT(mp, mnt_list);
742 sp->f_flags = mp->mnt_flag & MNT_VISFLAGMASK;
743 error = copyout((caddr_t)sp, sfsp, sizeof(*sp));
751 simple_lock(&mountlist_slock);
752 nmp = TAILQ_NEXT(mp, mnt_list);
755 simple_unlock(&mountlist_slock);
756 if (sfsp && count > maxcount)
757 p->p_retval[0] = maxcount;
759 p->p_retval[0] = count;
764 * fchdir_args(int fd)
766 * Change current working directory to a given file descriptor.
770 fchdir(struct fchdir_args *uap)
772 struct thread *td = curthread;
773 struct proc *p = td->td_proc;
774 struct filedesc *fdp = p->p_fd;
775 struct vnode *vp, *tdp;
780 if ((error = getvnode(fdp, SCARG(uap, fd), &fp)) != 0)
782 vp = (struct vnode *)fp->f_data;
784 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY, td);
785 if (vp->v_type != VDIR)
788 error = VOP_ACCESS(vp, VEXEC, p->p_ucred, td);
789 while (!error && (mp = vp->v_mountedhere) != NULL) {
790 if (vfs_busy(mp, 0, 0, td))
792 error = VFS_ROOT(mp, &tdp);
803 VOP_UNLOCK(vp, 0, td);
810 * chdir_args(char *path)
812 * Change current working directory (``.'').
816 chdir(struct chdir_args *uap)
818 struct thread *td = curthread;
819 struct proc *p = td->td_proc;
820 struct filedesc *fdp = p->p_fd;
824 NDINIT(&nd, LOOKUP, FOLLOW | LOCKLEAF, UIO_USERSPACE,
825 SCARG(uap, path), td);
826 if ((error = change_dir(&nd, td)) != 0)
828 NDFREE(&nd, NDF_ONLY_PNBUF);
830 fdp->fd_cdir = nd.ni_vp;
835 * Helper function for raised chroot(2) security function: Refuse if
836 * any filedescriptors are open directories.
839 chroot_refuse_vdir_fds(fdp)
840 struct filedesc *fdp;
847 for (fd = 0; fd < fdp->fd_nfiles ; fd++) {
848 error = getvnode(fdp, fd, &fp);
851 vp = (struct vnode *)fp->f_data;
852 if (vp->v_type != VDIR)
860 * This sysctl determines if we will allow a process to chroot(2) if it
861 * has a directory open:
862 * 0: disallowed for all processes.
863 * 1: allowed for processes that were not already chroot(2)'ed.
864 * 2: allowed for all processes.
867 static int chroot_allow_open_directories = 1;
869 SYSCTL_INT(_kern, OID_AUTO, chroot_allow_open_directories, CTLFLAG_RW,
870 &chroot_allow_open_directories, 0, "");
873 * chroot_args(char *path)
875 * Change notion of root (``/'') directory.
879 chroot(struct chroot_args *uap)
881 struct thread *td = curthread;
882 struct proc *p = td->td_proc;
883 struct filedesc *fdp = p->p_fd;
888 error = suser_cred(p->p_ucred, PRISON_ROOT);
891 if (chroot_allow_open_directories == 0 ||
892 (chroot_allow_open_directories == 1 && fdp->fd_rdir != rootvnode))
893 error = chroot_refuse_vdir_fds(fdp);
896 NDINIT(&nd, LOOKUP, FOLLOW | LOCKLEAF, UIO_USERSPACE,
897 SCARG(uap, path), td);
898 if ((error = change_dir(&nd, td)) != 0)
900 NDFREE(&nd, NDF_ONLY_PNBUF);
902 fdp->fd_rdir = nd.ni_vp;
904 fdp->fd_jdir = nd.ni_vp;
911 * Common routine for chroot and chdir.
914 change_dir(struct nameidata *ndp, struct thread *td)
923 if (vp->v_type != VDIR)
926 error = VOP_ACCESS(vp, VEXEC, ndp->ni_cnd.cn_cred, td);
930 VOP_UNLOCK(vp, 0, td);
935 * open_args(char *path, int flags, int mode)
937 * Check permissions, allocate an open file structure,
938 * and call the device open routine if any.
941 open(struct open_args *uap)
943 struct thread *td = curthread;
944 struct proc *p = td->td_proc;
945 struct filedesc *fdp = p->p_fd;
948 int cmode, flags, oflags;
950 int type, indx, error;
954 oflags = SCARG(uap, flags);
955 if ((oflags & O_ACCMODE) == O_ACCMODE)
957 flags = FFLAGS(oflags);
958 error = falloc(p, &nfp, &indx);
962 cmode = ((SCARG(uap, mode) &~ fdp->fd_cmask) & ALLPERMS) &~ S_ISTXT;
963 NDINIT(&nd, LOOKUP, FOLLOW, UIO_USERSPACE, SCARG(uap, path), td);
964 p->p_dupfd = -indx - 1; /* XXX check for fdopen */
966 * Bump the ref count to prevent another process from closing
967 * the descriptor while we are blocked in vn_open()
970 error = vn_open(&nd, flags, cmode);
973 * release our own reference
978 * handle special fdopen() case. bleh. dupfdopen() is
979 * responsible for dropping the old contents of ofiles[indx]
982 if ((error == ENODEV || error == ENXIO) &&
983 p->p_dupfd >= 0 && /* XXX from fdopen */
985 dupfdopen(fdp, indx, p->p_dupfd, flags, error)) == 0) {
986 p->p_retval[0] = indx;
990 * Clean up the descriptor, but only if another thread hadn't
991 * replaced or closed it.
993 if (fdp->fd_ofiles[indx] == fp) {
994 fdp->fd_ofiles[indx] = NULL;
998 if (error == ERESTART)
1003 NDFREE(&nd, NDF_ONLY_PNBUF);
1007 * There should be 2 references on the file, one from the descriptor
1008 * table, and one for us.
1010 * Handle the case where someone closed the file (via its file
1011 * descriptor) while we were blocked. The end result should look
1012 * like opening the file succeeded but it was immediately closed.
1014 if (fp->f_count == 1) {
1015 KASSERT(fdp->fd_ofiles[indx] != fp,
1016 ("Open file descriptor lost all refs"));
1017 VOP_UNLOCK(vp, 0, td);
1018 vn_close(vp, flags & FMASK, td);
1020 p->p_retval[0] = indx;
1024 fp->f_data = (caddr_t)vp;
1025 fp->f_flag = flags & FMASK;
1027 fp->f_type = (vp->v_type == VFIFO ? DTYPE_FIFO : DTYPE_VNODE);
1028 if (flags & (O_EXLOCK | O_SHLOCK)) {
1029 lf.l_whence = SEEK_SET;
1032 if (flags & O_EXLOCK)
1033 lf.l_type = F_WRLCK;
1035 lf.l_type = F_RDLCK;
1037 if ((flags & FNONBLOCK) == 0)
1039 VOP_UNLOCK(vp, 0, td);
1040 if ((error = VOP_ADVLOCK(vp, (caddr_t)fp, F_SETLK, &lf, type)) != 0) {
1042 * lock request failed. Normally close the descriptor
1043 * but handle the case where someone might have dup()d
1044 * it when we weren't looking. One reference is
1045 * owned by the descriptor array, the other by us.
1047 if (fdp->fd_ofiles[indx] == fp) {
1048 fdp->fd_ofiles[indx] = NULL;
1054 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY, td);
1055 fp->f_flag |= FHASLOCK;
1057 /* assert that vn_open created a backing object if one is needed */
1058 KASSERT(!vn_canvmio(vp) || VOP_GETVOBJECT(vp, NULL) == 0,
1059 ("open: vmio vnode has no backing object after vn_open"));
1060 VOP_UNLOCK(vp, 0, td);
1063 * release our private reference, leaving the one associated with the
1064 * descriptor table intact.
1067 p->p_retval[0] = indx;
1073 * ocreat(char *path, int mode)
1078 ocreat(struct ocreat_args *uap)
1080 struct open_args /* {
1081 syscallarg(char *) path;
1082 syscallarg(int) flags;
1083 syscallarg(int) mode;
1086 SCARG(&nuap, path) = SCARG(uap, path);
1087 SCARG(&nuap, mode) = SCARG(uap, mode);
1088 SCARG(&nuap, flags) = O_WRONLY | O_CREAT | O_TRUNC;
1089 return (open(&nuap));
1091 #endif /* COMPAT_43 */
1094 * mknod_args(char *path, int mode, int dev)
1096 * Create a special file.
1100 mknod(struct mknod_args *uap)
1102 struct thread *td = curthread;
1103 struct proc *p = td->td_proc;
1108 struct nameidata nd;
1112 switch (SCARG(uap, mode) & S_IFMT) {
1118 error = suser_cred(p->p_ucred, PRISON_ROOT);
1124 NDINIT(&nd, CREATE, LOCKPARENT, UIO_USERSPACE, SCARG(uap, path), td);
1125 if ((error = namei(&nd)) != 0)
1132 vattr.va_mode = (SCARG(uap, mode) & ALLPERMS) &~ p->p_fd->fd_cmask;
1133 vattr.va_rdev = SCARG(uap, dev);
1136 switch (SCARG(uap, mode) & S_IFMT) {
1137 case S_IFMT: /* used by badsect to flag bad sectors */
1138 vattr.va_type = VBAD;
1141 vattr.va_type = VCHR;
1144 vattr.va_type = VBLK;
1155 VOP_LEASE(nd.ni_dvp, td, p->p_ucred, LEASE_WRITE);
1157 error = VOP_WHITEOUT(nd.ni_dvp, &nd.ni_cnd, CREATE);
1159 error = VOP_MKNOD(nd.ni_dvp, &nd.ni_vp,
1160 &nd.ni_cnd, &vattr);
1164 NDFREE(&nd, NDF_ONLY_PNBUF);
1167 NDFREE(&nd, NDF_ONLY_PNBUF);
1168 if (nd.ni_dvp == vp)
1175 ASSERT_VOP_UNLOCKED(nd.ni_dvp, "mknod");
1176 ASSERT_VOP_UNLOCKED(nd.ni_vp, "mknod");
1181 * mkfifo_args(char *path, int mode)
1183 * Create a named pipe.
1187 mkfifo(struct mkfifo_args *uap)
1189 struct thread *td = curthread;
1190 struct proc *p = td->td_proc;
1193 struct nameidata nd;
1196 NDINIT(&nd, CREATE, LOCKPARENT, UIO_USERSPACE, SCARG(uap, path), td);
1197 if ((error = namei(&nd)) != 0)
1199 if (nd.ni_vp != NULL) {
1200 NDFREE(&nd, NDF_ONLY_PNBUF);
1201 if (nd.ni_dvp == nd.ni_vp)
1209 vattr.va_type = VFIFO;
1210 vattr.va_mode = (SCARG(uap, mode) & ALLPERMS) &~ p->p_fd->fd_cmask;
1211 VOP_LEASE(nd.ni_dvp, td, p->p_ucred, LEASE_WRITE);
1212 error = VOP_MKNOD(nd.ni_dvp, &nd.ni_vp, &nd.ni_cnd, &vattr);
1215 NDFREE(&nd, NDF_ONLY_PNBUF);
1221 * link_args(char *path, char *link)
1223 * Make a hard file link.
1227 link(struct link_args *uap)
1229 struct thread *td = curthread;
1230 struct proc *p = td->td_proc;
1232 struct nameidata nd;
1236 NDINIT(&nd, LOOKUP, FOLLOW|NOOBJ, UIO_USERSPACE, SCARG(uap, path), td);
1237 if ((error = namei(&nd)) != 0)
1239 NDFREE(&nd, NDF_ONLY_PNBUF);
1241 if (vp->v_type == VDIR)
1242 error = EPERM; /* POSIX */
1244 NDINIT(&nd, CREATE, LOCKPARENT|NOOBJ, UIO_USERSPACE, SCARG(uap, link), td);
1247 if (nd.ni_vp != NULL) {
1252 VOP_LEASE(nd.ni_dvp, td, p->p_ucred,
1254 VOP_LEASE(vp, td, p->p_ucred, LEASE_WRITE);
1255 error = VOP_LINK(nd.ni_dvp, vp, &nd.ni_cnd);
1257 NDFREE(&nd, NDF_ONLY_PNBUF);
1258 if (nd.ni_dvp == nd.ni_vp)
1265 ASSERT_VOP_UNLOCKED(nd.ni_dvp, "link");
1266 ASSERT_VOP_UNLOCKED(nd.ni_vp, "link");
1271 * symlink(char *path, char *link)
1273 * Make a symbolic link.
1277 symlink(struct symlink_args *uap)
1279 struct thread *td = curthread;
1280 struct proc *p = td->td_proc;
1284 struct nameidata nd;
1286 path = zalloc(namei_zone);
1287 if ((error = copyinstr(SCARG(uap, path), path, MAXPATHLEN, NULL)) != 0)
1290 NDINIT(&nd, CREATE, LOCKPARENT|NOOBJ, UIO_USERSPACE, SCARG(uap, link), td);
1291 if ((error = namei(&nd)) != 0)
1294 NDFREE(&nd, NDF_ONLY_PNBUF);
1295 if (nd.ni_dvp == nd.ni_vp)
1304 vattr.va_mode = ACCESSPERMS &~ p->p_fd->fd_cmask;
1305 VOP_LEASE(nd.ni_dvp, td, p->p_ucred, LEASE_WRITE);
1306 error = VOP_SYMLINK(nd.ni_dvp, &nd.ni_vp, &nd.ni_cnd, &vattr, path);
1307 NDFREE(&nd, NDF_ONLY_PNBUF);
1311 ASSERT_VOP_UNLOCKED(nd.ni_dvp, "symlink");
1312 ASSERT_VOP_UNLOCKED(nd.ni_vp, "symlink");
1314 zfree(namei_zone, path);
1319 * undelete_args(char *path)
1321 * Delete a whiteout from the filesystem.
1325 undelete(struct undelete_args *uap)
1327 struct thread *td = curthread;
1328 struct proc *p = td->td_proc;
1330 struct nameidata nd;
1333 NDINIT(&nd, DELETE, LOCKPARENT|DOWHITEOUT, UIO_USERSPACE,
1334 SCARG(uap, path), td);
1339 if (nd.ni_vp != NULLVP || !(nd.ni_cnd.cn_flags & ISWHITEOUT)) {
1340 NDFREE(&nd, NDF_ONLY_PNBUF);
1341 if (nd.ni_dvp == nd.ni_vp)
1350 VOP_LEASE(nd.ni_dvp, td, p->p_ucred, LEASE_WRITE);
1351 error = VOP_WHITEOUT(nd.ni_dvp, &nd.ni_cnd, DELETE);
1352 NDFREE(&nd, NDF_ONLY_PNBUF);
1354 ASSERT_VOP_UNLOCKED(nd.ni_dvp, "undelete");
1355 ASSERT_VOP_UNLOCKED(nd.ni_vp, "undelete");
1360 * unlink_args(char *path)
1362 * Delete a name from the filesystem.
1365 unlink(struct unlink_args *uap)
1367 struct thread *td = curthread;
1368 struct proc *p = td->td_proc;
1371 struct nameidata nd;
1374 NDINIT(&nd, DELETE, LOCKPARENT, UIO_USERSPACE, SCARG(uap, path), td);
1375 if ((error = namei(&nd)) != 0)
1378 VOP_LEASE(vp, td, p->p_ucred, LEASE_WRITE);
1379 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY, td);
1381 if (vp->v_type == VDIR)
1382 error = EPERM; /* POSIX */
1385 * The root of a mounted filesystem cannot be deleted.
1387 * XXX: can this only be a VDIR case?
1389 if (vp->v_flag & VROOT)
1394 VOP_LEASE(nd.ni_dvp, td, p->p_ucred, LEASE_WRITE);
1395 error = VOP_REMOVE(nd.ni_dvp, vp, &nd.ni_cnd);
1397 NDFREE(&nd, NDF_ONLY_PNBUF);
1398 if (nd.ni_dvp == vp)
1404 ASSERT_VOP_UNLOCKED(nd.ni_dvp, "unlink");
1405 ASSERT_VOP_UNLOCKED(nd.ni_vp, "unlink");
1410 * lseek_args(int fd, int pad, off_t offset, int whence)
1412 * Reposition read/write file offset.
1415 lseek(struct lseek_args *uap)
1417 struct thread *td = curthread;
1418 struct proc *p = td->td_proc;
1419 struct filedesc *fdp = p->p_fd;
1424 if ((u_int)SCARG(uap, fd) >= fdp->fd_nfiles ||
1425 (fp = fdp->fd_ofiles[SCARG(uap, fd)]) == NULL)
1427 if (fp->f_type != DTYPE_VNODE)
1429 switch (SCARG(uap, whence)) {
1431 fp->f_offset += SCARG(uap, offset);
1434 error=VOP_GETATTR((struct vnode *)fp->f_data, &vattr, td);
1437 fp->f_offset = SCARG(uap, offset) + vattr.va_size;
1440 fp->f_offset = SCARG(uap, offset);
1445 *(off_t *)(p->p_retval) = fp->f_offset;
1449 #if defined(COMPAT_43) || defined(COMPAT_SUNOS)
1451 * Reposition read/write file offset.
1453 * olseek_args(int fd, long offset, int whence)
1456 olseek(struct olseek_args *uap)
1458 struct lseek_args /* {
1460 syscallarg(int) pad;
1461 syscallarg(off_t) offset;
1462 syscallarg(int) whence;
1466 SCARG(&nuap, fd) = SCARG(uap, fd);
1467 SCARG(&nuap, offset) = SCARG(uap, offset);
1468 SCARG(&nuap, whence) = SCARG(uap, whence);
1469 error = lseek(&nuap);
1472 #endif /* COMPAT_43 */
1475 * access_args(char *path, int flags)
1477 * Check access permissions.
1480 access(struct access_args *uap)
1482 struct thread *td = curthread;
1483 struct proc *p = td->td_proc;
1484 struct ucred *cred, *tmpcred;
1487 struct nameidata nd;
1491 * Create and modify a temporary credential instead of one that
1492 * is potentially shared. This could also mess up socket
1493 * buffer accounting which can run in an interrupt context.
1495 tmpcred = crdup(cred);
1496 tmpcred->cr_uid = p->p_ucred->cr_ruid;
1497 tmpcred->cr_groups[0] = p->p_ucred->cr_rgid;
1498 p->p_ucred = tmpcred;
1499 NDINIT(&nd, LOOKUP, FOLLOW | LOCKLEAF | NOOBJ, UIO_USERSPACE,
1500 SCARG(uap, path), td);
1501 if ((error = namei(&nd)) != 0)
1505 /* Flags == 0 means only check for existence. */
1506 if (SCARG(uap, flags)) {
1508 if (SCARG(uap, flags) & R_OK)
1510 if (SCARG(uap, flags) & W_OK)
1512 if (SCARG(uap, flags) & X_OK)
1514 if ((flags & VWRITE) == 0 || (error = vn_writechk(vp)) == 0)
1515 error = VOP_ACCESS(vp, flags, tmpcred, td);
1517 NDFREE(&nd, NDF_ONLY_PNBUF);
1525 #if defined(COMPAT_43) || defined(COMPAT_SUNOS)
1527 * ostat_args(char *path, struct ostat *ub)
1529 * Get file status; this version follows links.
1533 ostat(struct ostat_args *uap)
1535 struct thread *td = curthread;
1539 struct nameidata nd;
1541 NDINIT(&nd, LOOKUP, FOLLOW | LOCKLEAF | NOOBJ, UIO_USERSPACE,
1542 SCARG(uap, path), td);
1543 if ((error = namei(&nd)) != 0)
1545 NDFREE(&nd, NDF_ONLY_PNBUF);
1546 error = vn_stat(nd.ni_vp, &sb, td);
1551 error = copyout((caddr_t)&osb, (caddr_t)SCARG(uap, ub), sizeof (osb));
1556 * olstat_args(char *path, struct ostat *ub)
1558 * Get file status; this version does not follow links.
1562 olstat(struct olstat_args *uap)
1564 struct thread *td = curthread;
1569 struct nameidata nd;
1571 NDINIT(&nd, LOOKUP, NOFOLLOW | LOCKLEAF | NOOBJ, UIO_USERSPACE,
1572 SCARG(uap, path), td);
1573 if ((error = namei(&nd)) != 0)
1576 error = vn_stat(vp, &sb, td);
1577 NDFREE(&nd, NDF_ONLY_PNBUF);
1582 error = copyout((caddr_t)&osb, (caddr_t)SCARG(uap, ub), sizeof (osb));
1587 * Convert from an old to a new stat structure.
1594 ost->st_dev = st->st_dev;
1595 ost->st_ino = st->st_ino;
1596 ost->st_mode = st->st_mode;
1597 ost->st_nlink = st->st_nlink;
1598 ost->st_uid = st->st_uid;
1599 ost->st_gid = st->st_gid;
1600 ost->st_rdev = st->st_rdev;
1601 if (st->st_size < (quad_t)1 << 32)
1602 ost->st_size = st->st_size;
1605 ost->st_atime = st->st_atime;
1606 ost->st_mtime = st->st_mtime;
1607 ost->st_ctime = st->st_ctime;
1608 ost->st_blksize = st->st_blksize;
1609 ost->st_blocks = st->st_blocks;
1610 ost->st_flags = st->st_flags;
1611 ost->st_gen = st->st_gen;
1613 #endif /* COMPAT_43 || COMPAT_SUNOS */
1616 * stat_args(char *path, struct stat *ub)
1618 * Get file status; this version follows links.
1622 stat(struct stat_args *uap)
1624 struct thread *td = curthread;
1627 struct nameidata nd;
1629 NDINIT(&nd, LOOKUP, FOLLOW | LOCKLEAF | NOOBJ, UIO_USERSPACE,
1630 SCARG(uap, path), td);
1631 if ((error = namei(&nd)) != 0)
1633 error = vn_stat(nd.ni_vp, &sb, td);
1634 NDFREE(&nd, NDF_ONLY_PNBUF);
1638 error = copyout((caddr_t)&sb, (caddr_t)SCARG(uap, ub), sizeof (sb));
1643 * lstat_args(char *path, struct stat *ub)
1645 * Get file status; this version does not follow links.
1649 lstat(struct lstat_args *uap)
1651 struct thread *td = curthread;
1655 struct nameidata nd;
1657 NDINIT(&nd, LOOKUP, NOFOLLOW | LOCKLEAF | NOOBJ, UIO_USERSPACE,
1658 SCARG(uap, path), td);
1659 if ((error = namei(&nd)) != 0)
1662 error = vn_stat(vp, &sb, td);
1663 NDFREE(&nd, NDF_ONLY_PNBUF);
1667 error = copyout((caddr_t)&sb, (caddr_t)SCARG(uap, ub), sizeof (sb));
1676 nsb->st_dev = sb->st_dev;
1677 nsb->st_ino = sb->st_ino;
1678 nsb->st_mode = sb->st_mode;
1679 nsb->st_nlink = sb->st_nlink;
1680 nsb->st_uid = sb->st_uid;
1681 nsb->st_gid = sb->st_gid;
1682 nsb->st_rdev = sb->st_rdev;
1683 nsb->st_atimespec = sb->st_atimespec;
1684 nsb->st_mtimespec = sb->st_mtimespec;
1685 nsb->st_ctimespec = sb->st_ctimespec;
1686 nsb->st_size = sb->st_size;
1687 nsb->st_blocks = sb->st_blocks;
1688 nsb->st_blksize = sb->st_blksize;
1689 nsb->st_flags = sb->st_flags;
1690 nsb->st_gen = sb->st_gen;
1691 nsb->st_qspare[0] = sb->st_qspare[0];
1692 nsb->st_qspare[1] = sb->st_qspare[1];
1696 * nstat_args(char *path, struct nstat *ub)
1700 nstat(struct nstat_args *uap)
1702 struct thread *td = curthread;
1706 struct nameidata nd;
1708 NDINIT(&nd, LOOKUP, FOLLOW | LOCKLEAF | NOOBJ, UIO_USERSPACE,
1709 SCARG(uap, path), td);
1710 if ((error = namei(&nd)) != 0)
1712 NDFREE(&nd, NDF_ONLY_PNBUF);
1713 error = vn_stat(nd.ni_vp, &sb, td);
1717 cvtnstat(&sb, &nsb);
1718 error = copyout((caddr_t)&nsb, (caddr_t)SCARG(uap, ub), sizeof (nsb));
1723 * lstat_args(char *path, struct stat *ub)
1725 * Get file status; this version does not follow links.
1729 nlstat(struct nlstat_args *uap)
1731 struct thread *td = curthread;
1736 struct nameidata nd;
1738 NDINIT(&nd, LOOKUP, NOFOLLOW | LOCKLEAF | NOOBJ, UIO_USERSPACE,
1739 SCARG(uap, path), td);
1740 if ((error = namei(&nd)) != 0)
1743 NDFREE(&nd, NDF_ONLY_PNBUF);
1744 error = vn_stat(vp, &sb, td);
1748 cvtnstat(&sb, &nsb);
1749 error = copyout((caddr_t)&nsb, (caddr_t)SCARG(uap, ub), sizeof (nsb));
1754 * pathconf_Args(char *path, int name)
1756 * Get configurable pathname variables.
1760 pathconf(struct pathconf_args *uap)
1762 struct thread *td = curthread;
1763 struct proc *p = td->td_proc;
1765 struct nameidata nd;
1767 NDINIT(&nd, LOOKUP, FOLLOW | LOCKLEAF | NOOBJ, UIO_USERSPACE,
1768 SCARG(uap, path), td);
1769 if ((error = namei(&nd)) != 0)
1771 NDFREE(&nd, NDF_ONLY_PNBUF);
1772 error = VOP_PATHCONF(nd.ni_vp, SCARG(uap, name), p->p_retval);
1778 * readlink_args(char *path, char *buf, int count)
1780 * Return target name of a symbolic link.
1784 readlink(struct readlink_args *uap)
1786 struct thread *td = curthread;
1787 struct proc *p = td->td_proc;
1792 struct nameidata nd;
1794 NDINIT(&nd, LOOKUP, NOFOLLOW | LOCKLEAF | NOOBJ, UIO_USERSPACE,
1795 SCARG(uap, path), td);
1796 if ((error = namei(&nd)) != 0)
1798 NDFREE(&nd, NDF_ONLY_PNBUF);
1800 if (vp->v_type != VLNK)
1803 aiov.iov_base = SCARG(uap, buf);
1804 aiov.iov_len = SCARG(uap, count);
1805 auio.uio_iov = &aiov;
1806 auio.uio_iovcnt = 1;
1807 auio.uio_offset = 0;
1808 auio.uio_rw = UIO_READ;
1809 auio.uio_segflg = UIO_USERSPACE;
1811 auio.uio_resid = SCARG(uap, count);
1812 error = VOP_READLINK(vp, &auio, p->p_ucred);
1815 p->p_retval[0] = SCARG(uap, count) - auio.uio_resid;
1820 setfflags(struct vnode *vp, int flags)
1822 struct thread *td = curthread;
1823 struct proc *p = td->td_proc;
1828 * Prevent non-root users from setting flags on devices. When
1829 * a device is reused, users can retain ownership of the device
1830 * if they are allowed to set flags and programs assume that
1831 * chown can't fail when done as root.
1833 if ((vp->v_type == VCHR || vp->v_type == VBLK) &&
1834 ((error = suser_cred(p->p_ucred, PRISON_ROOT)) != 0))
1837 VOP_LEASE(vp, td, p->p_ucred, LEASE_WRITE);
1838 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY, td);
1840 vattr.va_flags = flags;
1841 error = VOP_SETATTR(vp, &vattr, p->p_ucred, td);
1842 VOP_UNLOCK(vp, 0, td);
1847 * chflags(char *path, int flags)
1849 * Change flags of a file given a path name.
1853 chflags(struct chflags_args *uap)
1855 struct thread *td = curthread;
1857 struct nameidata nd;
1859 NDINIT(&nd, LOOKUP, FOLLOW, UIO_USERSPACE, SCARG(uap, path), td);
1860 if ((error = namei(&nd)) != 0)
1862 NDFREE(&nd, NDF_ONLY_PNBUF);
1863 error = setfflags(nd.ni_vp, SCARG(uap, flags));
1869 * fchflags_args(int fd, int flags)
1871 * Change flags of a file given a file descriptor.
1875 fchflags(struct fchflags_args *uap)
1877 struct thread *td = curthread;
1878 struct proc *p = td->td_proc;
1882 if ((error = getvnode(p->p_fd, SCARG(uap, fd), &fp)) != 0)
1884 return setfflags((struct vnode *) fp->f_data, SCARG(uap, flags));
1888 setfmode(struct vnode *vp, int mode)
1890 struct thread *td = curthread;
1891 struct proc *p = td->td_proc;
1895 VOP_LEASE(vp, td, p->p_ucred, LEASE_WRITE);
1896 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY, td);
1898 vattr.va_mode = mode & ALLPERMS;
1899 error = VOP_SETATTR(vp, &vattr, p->p_ucred, td);
1900 VOP_UNLOCK(vp, 0, td);
1905 * chmod_args(char *path, int mode)
1907 * Change mode of a file given path name.
1911 chmod(struct chmod_args *uap)
1913 struct thread *td = curthread;
1915 struct nameidata nd;
1917 NDINIT(&nd, LOOKUP, FOLLOW, UIO_USERSPACE, SCARG(uap, path), td);
1918 if ((error = namei(&nd)) != 0)
1920 NDFREE(&nd, NDF_ONLY_PNBUF);
1921 error = setfmode(nd.ni_vp, SCARG(uap, mode));
1927 * lchmod_args(char *path, int mode)
1929 * Change mode of a file given path name (don't follow links.)
1933 lchmod(struct lchmod_args *uap)
1935 struct thread *td = curthread;
1937 struct nameidata nd;
1939 NDINIT(&nd, LOOKUP, NOFOLLOW, UIO_USERSPACE, SCARG(uap, path), td);
1940 if ((error = namei(&nd)) != 0)
1942 NDFREE(&nd, NDF_ONLY_PNBUF);
1943 error = setfmode(nd.ni_vp, SCARG(uap, mode));
1949 * fchmod_args(int fd, int mode)
1951 * Change mode of a file given a file descriptor.
1955 fchmod(struct fchmod_args *uap)
1957 struct thread *td = curthread;
1958 struct proc *p = td->td_proc;
1962 if ((error = getvnode(p->p_fd, SCARG(uap, fd), &fp)) != 0)
1964 return setfmode((struct vnode *)fp->f_data, SCARG(uap, mode));
1968 setfown(struct vnode *vp, uid_t uid, gid_t gid)
1970 struct thread *td = curthread;
1971 struct proc *p = td->td_proc;
1975 VOP_LEASE(vp, td, p->p_ucred, LEASE_WRITE);
1976 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY, td);
1980 error = VOP_SETATTR(vp, &vattr, p->p_ucred, td);
1981 VOP_UNLOCK(vp, 0, td);
1986 * chown(char *path, int uid, int gid)
1988 * Set ownership given a path name.
1992 chown(struct chown_args *uap)
1994 struct thread *td = curthread;
1996 struct nameidata nd;
1998 NDINIT(&nd, LOOKUP, FOLLOW, UIO_USERSPACE, SCARG(uap, path), td);
1999 if ((error = namei(&nd)) != 0)
2001 NDFREE(&nd, NDF_ONLY_PNBUF);
2002 error = setfown(nd.ni_vp, SCARG(uap, uid), SCARG(uap, gid));
2008 * lchown_args(char *path, int uid, int gid)
2010 * Set ownership given a path name, do not cross symlinks.
2014 lchown(struct lchown_args *uap)
2016 struct thread *td = curthread;
2018 struct nameidata nd;
2020 NDINIT(&nd, LOOKUP, NOFOLLOW, UIO_USERSPACE, SCARG(uap, path), td);
2021 if ((error = namei(&nd)) != 0)
2023 NDFREE(&nd, NDF_ONLY_PNBUF);
2024 error = setfown(nd.ni_vp, SCARG(uap, uid), SCARG(uap, gid));
2030 * fchown_args(int fd, int uid, int gid)
2032 * Set ownership given a file descriptor.
2036 fchown(struct fchown_args *uap)
2038 struct thread *td = curthread;
2039 struct proc *p = td->td_proc;
2043 if ((error = getvnode(p->p_fd, SCARG(uap, fd), &fp)) != 0)
2045 return setfown((struct vnode *)fp->f_data,
2046 SCARG(uap, uid), SCARG(uap, gid));
2050 getutimes(const struct timeval *usrtvp, struct timespec *tsp)
2052 struct timeval tv[2];
2055 if (usrtvp == NULL) {
2057 TIMEVAL_TO_TIMESPEC(&tv[0], &tsp[0]);
2060 if ((error = copyin(usrtvp, tv, sizeof (tv))) != 0)
2062 TIMEVAL_TO_TIMESPEC(&tv[0], &tsp[0]);
2063 TIMEVAL_TO_TIMESPEC(&tv[1], &tsp[1]);
2069 setutimes(struct vnode *vp, const struct timespec *ts, int nullflag)
2071 struct thread *td = curthread;
2072 struct proc *p = td->td_proc;
2076 VOP_LEASE(vp, td, p->p_ucred, LEASE_WRITE);
2077 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY, td);
2079 vattr.va_atime = ts[0];
2080 vattr.va_mtime = ts[1];
2082 vattr.va_vaflags |= VA_UTIMES_NULL;
2083 error = VOP_SETATTR(vp, &vattr, p->p_ucred, td);
2084 VOP_UNLOCK(vp, 0, td);
2089 * utimes_args(char *path, struct timeval *tptr)
2091 * Set the access and modification times of a file.
2095 utimes(struct utimes_args *uap)
2097 struct thread *td = curthread;
2098 struct timespec ts[2];
2099 struct timeval *usrtvp;
2101 struct nameidata nd;
2103 usrtvp = SCARG(uap, tptr);
2104 if ((error = getutimes(usrtvp, ts)) != 0)
2106 NDINIT(&nd, LOOKUP, FOLLOW, UIO_USERSPACE, SCARG(uap, path), td);
2107 if ((error = namei(&nd)) != 0)
2109 NDFREE(&nd, NDF_ONLY_PNBUF);
2110 error = setutimes(nd.ni_vp, ts, usrtvp == NULL);
2116 * lutimes_args(char *path, struct timeval *tptr)
2118 * Set the access and modification times of a file.
2122 lutimes(struct lutimes_args *uap)
2124 struct thread *td = curthread;
2125 struct timespec ts[2];
2126 struct timeval *usrtvp;
2128 struct nameidata nd;
2130 usrtvp = SCARG(uap, tptr);
2131 if ((error = getutimes(usrtvp, ts)) != 0)
2133 NDINIT(&nd, LOOKUP, NOFOLLOW, UIO_USERSPACE, SCARG(uap, path), td);
2134 if ((error = namei(&nd)) != 0)
2136 NDFREE(&nd, NDF_ONLY_PNBUF);
2137 error = setutimes(nd.ni_vp, ts, usrtvp == NULL);
2143 * futimes_args(int fd, struct timeval *tptr)
2145 * Set the access and modification times of a file.
2149 futimes(struct futimes_args *uap)
2151 struct thread *td = curthread;
2152 struct proc *p = td->td_proc;
2153 struct timespec ts[2];
2155 struct timeval *usrtvp;
2158 usrtvp = SCARG(uap, tptr);
2159 if ((error = getutimes(usrtvp, ts)) != 0)
2161 if ((error = getvnode(p->p_fd, SCARG(uap, fd), &fp)) != 0)
2163 return setutimes((struct vnode *)fp->f_data, ts, usrtvp == NULL);
2167 * truncate(char *path, int pad, off_t length)
2169 * Truncate a file given its path name.
2173 truncate(struct truncate_args *uap)
2175 struct thread *td = curthread;
2176 struct proc *p = td->td_proc;
2180 struct nameidata nd;
2182 if (uap->length < 0)
2184 NDINIT(&nd, LOOKUP, FOLLOW, UIO_USERSPACE, SCARG(uap, path), td);
2185 if ((error = namei(&nd)) != 0)
2188 NDFREE(&nd, NDF_ONLY_PNBUF);
2189 VOP_LEASE(vp, td, p->p_ucred, LEASE_WRITE);
2190 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY, td);
2191 if (vp->v_type == VDIR)
2193 else if ((error = vn_writechk(vp)) == 0 &&
2194 (error = VOP_ACCESS(vp, VWRITE, p->p_ucred, td)) == 0) {
2196 vattr.va_size = SCARG(uap, length);
2197 error = VOP_SETATTR(vp, &vattr, p->p_ucred, td);
2204 * ftruncate_args(int fd, int pad, off_t length)
2206 * Truncate a file given a file descriptor.
2210 ftruncate(struct ftruncate_args *uap)
2212 struct thread *td = curthread;
2213 struct proc *p = td->td_proc;
2219 if (uap->length < 0)
2221 if ((error = getvnode(p->p_fd, SCARG(uap, fd), &fp)) != 0)
2223 if ((fp->f_flag & FWRITE) == 0)
2225 vp = (struct vnode *)fp->f_data;
2226 VOP_LEASE(vp, td, p->p_ucred, LEASE_WRITE);
2227 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY, td);
2228 if (vp->v_type == VDIR)
2230 else if ((error = vn_writechk(vp)) == 0) {
2232 vattr.va_size = SCARG(uap, length);
2233 error = VOP_SETATTR(vp, &vattr, fp->f_cred, td);
2235 VOP_UNLOCK(vp, 0, td);
2239 #if defined(COMPAT_43) || defined(COMPAT_SUNOS)
2241 * otruncate_args(char *path, long length)
2243 * Truncate a file given its path name.
2247 otruncate(struct otruncate_args *uap)
2249 struct truncate_args /* {
2250 syscallarg(char *) path;
2251 syscallarg(int) pad;
2252 syscallarg(off_t) length;
2255 SCARG(&nuap, path) = SCARG(uap, path);
2256 SCARG(&nuap, length) = SCARG(uap, length);
2257 return (truncate(&nuap));
2261 * oftruncate_args(int fd, long length)
2263 * Truncate a file given a file descriptor.
2267 oftruncate(struct oftruncate_args *uap)
2269 struct ftruncate_args /* {
2271 syscallarg(int) pad;
2272 syscallarg(off_t) length;
2275 SCARG(&nuap, fd) = SCARG(uap, fd);
2276 SCARG(&nuap, length) = SCARG(uap, length);
2277 return (ftruncate(&nuap));
2279 #endif /* COMPAT_43 || COMPAT_SUNOS */
2284 * Sync an open file.
2288 fsync(struct fsync_args *uap)
2290 struct thread *td = curthread;
2291 struct proc *p = td->td_proc;
2297 if ((error = getvnode(p->p_fd, SCARG(uap, fd), &fp)) != 0)
2299 vp = (struct vnode *)fp->f_data;
2300 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY, td);
2301 if (VOP_GETVOBJECT(vp, &obj) == 0)
2302 vm_object_page_clean(obj, 0, 0, 0);
2303 if ((error = VOP_FSYNC(vp, MNT_WAIT, td)) == 0 &&
2304 vp->v_mount && (vp->v_mount->mnt_flag & MNT_SOFTDEP) &&
2306 error = (*bioops.io_fsync)(vp);
2307 VOP_UNLOCK(vp, 0, td);
2312 * rename_args(char *from, char *to)
2314 * Rename files. Source and destination must either both be directories,
2315 * or both not be directories. If target is a directory, it must be empty.
2319 rename(struct rename_args *uap)
2321 struct thread *td = curthread;
2322 struct proc *p = td->td_proc;
2323 struct vnode *tvp, *fvp, *tdvp;
2324 struct nameidata fromnd, tond;
2328 NDINIT(&fromnd, DELETE, WANTPARENT | SAVESTART, UIO_USERSPACE,
2329 SCARG(uap, from), td);
2330 if ((error = namei(&fromnd)) != 0)
2333 NDINIT(&tond, RENAME, LOCKPARENT | LOCKLEAF | NOCACHE | SAVESTART | NOOBJ,
2334 UIO_USERSPACE, SCARG(uap, to), td);
2335 if (fromnd.ni_vp->v_type == VDIR)
2336 tond.ni_cnd.cn_flags |= WILLBEDIR;
2337 if ((error = namei(&tond)) != 0) {
2338 /* Translate error code for rename("dir1", "dir2/."). */
2339 if (error == EISDIR && fvp->v_type == VDIR)
2341 NDFREE(&fromnd, NDF_ONLY_PNBUF);
2342 vrele(fromnd.ni_dvp);
2349 if (fvp->v_type == VDIR && tvp->v_type != VDIR) {
2352 } else if (fvp->v_type != VDIR && tvp->v_type == VDIR) {
2360 * If the source is the same as the destination (that is, if they
2361 * are links to the same vnode), then there is nothing to do.
2367 VOP_LEASE(tdvp, td, p->p_ucred, LEASE_WRITE);
2368 if (fromnd.ni_dvp != tdvp) {
2369 VOP_LEASE(fromnd.ni_dvp, td, p->p_ucred, LEASE_WRITE);
2372 VOP_LEASE(tvp, td, p->p_ucred, LEASE_WRITE);
2374 error = VOP_RENAME(fromnd.ni_dvp, fromnd.ni_vp, &fromnd.ni_cnd,
2375 tond.ni_dvp, tond.ni_vp, &tond.ni_cnd);
2376 NDFREE(&fromnd, NDF_ONLY_PNBUF);
2377 NDFREE(&tond, NDF_ONLY_PNBUF);
2379 NDFREE(&fromnd, NDF_ONLY_PNBUF);
2380 NDFREE(&tond, NDF_ONLY_PNBUF);
2387 vrele(fromnd.ni_dvp);
2390 vrele(tond.ni_startdir);
2391 ASSERT_VOP_UNLOCKED(fromnd.ni_dvp, "rename");
2392 ASSERT_VOP_UNLOCKED(fromnd.ni_vp, "rename");
2393 ASSERT_VOP_UNLOCKED(tond.ni_dvp, "rename");
2394 ASSERT_VOP_UNLOCKED(tond.ni_vp, "rename");
2396 if (fromnd.ni_startdir)
2397 vrele(fromnd.ni_startdir);
2404 * mkdir_args(char *path, int mode)
2406 * Make a directory file.
2410 mkdir(struct mkdir_args *uap)
2412 struct thread *td = curthread;
2413 struct proc *p = td->td_proc;
2417 struct nameidata nd;
2420 NDINIT(&nd, CREATE, LOCKPARENT, UIO_USERSPACE, SCARG(uap, path), td);
2421 nd.ni_cnd.cn_flags |= WILLBEDIR;
2422 if ((error = namei(&nd)) != 0)
2426 NDFREE(&nd, NDF_ONLY_PNBUF);
2427 if (nd.ni_dvp == vp)
2435 vattr.va_type = VDIR;
2436 vattr.va_mode = (SCARG(uap, mode) & ACCESSPERMS) &~ p->p_fd->fd_cmask;
2437 VOP_LEASE(nd.ni_dvp, td, p->p_ucred, LEASE_WRITE);
2438 error = VOP_MKDIR(nd.ni_dvp, &nd.ni_vp, &nd.ni_cnd, &vattr);
2439 NDFREE(&nd, NDF_ONLY_PNBUF);
2443 ASSERT_VOP_UNLOCKED(nd.ni_dvp, "mkdir");
2444 ASSERT_VOP_UNLOCKED(nd.ni_vp, "mkdir");
2449 * rmdir_args(char *path)
2451 * Remove a directory file.
2455 rmdir(struct rmdir_args *uap)
2457 struct thread *td = curthread;
2458 struct proc *p = td->td_proc;
2461 struct nameidata nd;
2464 NDINIT(&nd, DELETE, LOCKPARENT | LOCKLEAF, UIO_USERSPACE,
2465 SCARG(uap, path), td);
2466 if ((error = namei(&nd)) != 0)
2469 if (vp->v_type != VDIR) {
2474 * No rmdir "." please.
2476 if (nd.ni_dvp == vp) {
2481 * The root of a mounted filesystem cannot be deleted.
2483 if (vp->v_flag & VROOT)
2486 VOP_LEASE(nd.ni_dvp, td, p->p_ucred, LEASE_WRITE);
2487 VOP_LEASE(vp, td, p->p_ucred, LEASE_WRITE);
2488 error = VOP_RMDIR(nd.ni_dvp, nd.ni_vp, &nd.ni_cnd);
2491 NDFREE(&nd, NDF_ONLY_PNBUF);
2492 if (nd.ni_dvp == vp)
2498 ASSERT_VOP_UNLOCKED(nd.ni_dvp, "rmdir");
2499 ASSERT_VOP_UNLOCKED(nd.ni_vp, "rmdir");
2505 * ogetdirentries_args(int fd, char *buf, u_int count, long *basep)
2507 * Read a block of directory entries in a file system independent format.
2510 ogetdirentries(struct ogetdirentries_args *uap)
2512 struct thread *td = curthread;
2513 struct proc *p = td->td_proc;
2516 struct uio auio, kuio;
2517 struct iovec aiov, kiov;
2518 struct dirent *dp, *edp;
2520 int error, eofflag, readcnt;
2523 /* XXX arbitrary sanity limit on `count'. */
2524 if (SCARG(uap, count) > 64 * 1024)
2526 if ((error = getvnode(p->p_fd, SCARG(uap, fd), &fp)) != 0)
2528 if ((fp->f_flag & FREAD) == 0)
2530 vp = (struct vnode *)fp->f_data;
2532 if (vp->v_type != VDIR)
2534 aiov.iov_base = SCARG(uap, buf);
2535 aiov.iov_len = SCARG(uap, count);
2536 auio.uio_iov = &aiov;
2537 auio.uio_iovcnt = 1;
2538 auio.uio_rw = UIO_READ;
2539 auio.uio_segflg = UIO_USERSPACE;
2541 auio.uio_resid = SCARG(uap, count);
2542 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY, td);
2543 loff = auio.uio_offset = fp->f_offset;
2544 # if (BYTE_ORDER != LITTLE_ENDIAN)
2545 if (vp->v_mount->mnt_maxsymlinklen <= 0) {
2546 error = VOP_READDIR(vp, &auio, fp->f_cred, &eofflag,
2548 fp->f_offset = auio.uio_offset;
2553 kuio.uio_iov = &kiov;
2554 kuio.uio_segflg = UIO_SYSSPACE;
2555 kiov.iov_len = SCARG(uap, count);
2556 MALLOC(dirbuf, caddr_t, SCARG(uap, count), M_TEMP, M_WAITOK);
2557 kiov.iov_base = dirbuf;
2558 error = VOP_READDIR(vp, &kuio, fp->f_cred, &eofflag,
2560 fp->f_offset = kuio.uio_offset;
2562 readcnt = SCARG(uap, count) - kuio.uio_resid;
2563 edp = (struct dirent *)&dirbuf[readcnt];
2564 for (dp = (struct dirent *)dirbuf; dp < edp; ) {
2565 # if (BYTE_ORDER == LITTLE_ENDIAN)
2567 * The expected low byte of
2568 * dp->d_namlen is our dp->d_type.
2569 * The high MBZ byte of dp->d_namlen
2570 * is our dp->d_namlen.
2572 dp->d_type = dp->d_namlen;
2576 * The dp->d_type is the high byte
2577 * of the expected dp->d_namlen,
2578 * so must be zero'ed.
2582 if (dp->d_reclen > 0) {
2583 dp = (struct dirent *)
2584 ((char *)dp + dp->d_reclen);
2591 error = uiomove(dirbuf, readcnt, &auio);
2593 FREE(dirbuf, M_TEMP);
2595 VOP_UNLOCK(vp, 0, td);
2598 if (SCARG(uap, count) == auio.uio_resid) {
2599 if (union_dircheckp) {
2600 error = union_dircheckp(td, &vp, fp);
2606 if ((vp->v_flag & VROOT) &&
2607 (vp->v_mount->mnt_flag & MNT_UNION)) {
2608 struct vnode *tvp = vp;
2609 vp = vp->v_mount->mnt_vnodecovered;
2611 fp->f_data = (caddr_t) vp;
2617 error = copyout((caddr_t)&loff, (caddr_t)SCARG(uap, basep),
2619 p->p_retval[0] = SCARG(uap, count) - auio.uio_resid;
2622 #endif /* COMPAT_43 */
2625 * getdirentries_args(int fd, char *buf, u_int conut, long *basep)
2627 * Read a block of directory entries in a file system independent format.
2630 getdirentries(struct getdirentries_args *uap)
2632 struct thread *td = curthread;
2633 struct proc *p = td->td_proc;
2641 if ((error = getvnode(p->p_fd, SCARG(uap, fd), &fp)) != 0)
2643 if ((fp->f_flag & FREAD) == 0)
2645 vp = (struct vnode *)fp->f_data;
2647 if (vp->v_type != VDIR)
2649 aiov.iov_base = SCARG(uap, buf);
2650 aiov.iov_len = SCARG(uap, count);
2651 auio.uio_iov = &aiov;
2652 auio.uio_iovcnt = 1;
2653 auio.uio_rw = UIO_READ;
2654 auio.uio_segflg = UIO_USERSPACE;
2656 auio.uio_resid = SCARG(uap, count);
2657 /* vn_lock(vp, LK_SHARED | LK_RETRY, td); */
2658 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY, td);
2659 loff = auio.uio_offset = fp->f_offset;
2660 error = VOP_READDIR(vp, &auio, fp->f_cred, &eofflag, NULL, NULL);
2661 fp->f_offset = auio.uio_offset;
2662 VOP_UNLOCK(vp, 0, td);
2665 if (SCARG(uap, count) == auio.uio_resid) {
2666 if (union_dircheckp) {
2667 error = union_dircheckp(td, &vp, fp);
2673 if ((vp->v_flag & VROOT) &&
2674 (vp->v_mount->mnt_flag & MNT_UNION)) {
2675 struct vnode *tvp = vp;
2676 vp = vp->v_mount->mnt_vnodecovered;
2678 fp->f_data = (caddr_t) vp;
2684 if (SCARG(uap, basep) != NULL) {
2685 error = copyout((caddr_t)&loff, (caddr_t)SCARG(uap, basep),
2688 p->p_retval[0] = SCARG(uap, count) - auio.uio_resid;
2693 * getdents_args(int fd, char *buf, size_t count)
2696 getdents(struct getdents_args *uap)
2698 struct getdirentries_args ap;
2702 ap.count = uap->count;
2704 return getdirentries(&ap);
2708 * umask(int newmask)
2710 * Set the mode mask for creation of filesystem nodes.
2715 umask(struct umask_args *uap)
2717 struct thread *td = curthread;
2718 struct proc *p = td->td_proc;
2719 struct filedesc *fdp;
2722 p->p_retval[0] = fdp->fd_cmask;
2723 fdp->fd_cmask = SCARG(uap, newmask) & ALLPERMS;
2728 * revoke(char *path)
2730 * Void all references to file by ripping underlying filesystem
2735 revoke(struct revoke_args *uap)
2737 struct thread *td = curthread;
2738 struct proc *p = td->td_proc;
2742 struct nameidata nd;
2744 NDINIT(&nd, LOOKUP, FOLLOW, UIO_USERSPACE, SCARG(uap, path), td);
2745 if ((error = namei(&nd)) != 0)
2748 NDFREE(&nd, NDF_ONLY_PNBUF);
2749 if (vp->v_type != VCHR && vp->v_type != VBLK) {
2753 if ((error = VOP_GETATTR(vp, &vattr, td)) != 0)
2755 if (p->p_ucred->cr_uid != vattr.va_uid &&
2756 (error = suser_cred(p->p_ucred, PRISON_ROOT)))
2759 VOP_REVOKE(vp, REVOKEALL);
2766 * Convert a user file descriptor to a kernel file entry.
2769 getvnode(struct filedesc *fdp, int fd, struct file **fpp)
2773 if ((u_int)fd >= fdp->fd_nfiles ||
2774 (fp = fdp->fd_ofiles[fd]) == NULL)
2776 if (fp->f_type != DTYPE_VNODE && fp->f_type != DTYPE_FIFO)
2782 * getfh_args(char *fname, fhandle_t *fhp)
2784 * Get (NFS) file handle
2787 getfh(struct getfh_args *uap)
2789 struct thread *td = curthread;
2790 struct nameidata nd;
2796 * Must be super user
2801 NDINIT(&nd, LOOKUP, FOLLOW | LOCKLEAF, UIO_USERSPACE, uap->fname, td);
2805 NDFREE(&nd, NDF_ONLY_PNBUF);
2807 bzero(&fh, sizeof(fh));
2808 fh.fh_fsid = vp->v_mount->mnt_stat.f_fsid;
2809 error = VFS_VPTOFH(vp, &fh.fh_fid);
2813 error = copyout(&fh, uap->fhp, sizeof (fh));
2818 * fhopen_args(const struct fhandle *u_fhp, int flags)
2820 * syscall for the rpc.lockd to use to translate a NFS file handle into
2821 * an open descriptor.
2823 * warning: do not remove the suser() call or this becomes one giant
2827 fhopen(struct fhopen_args *uap)
2829 struct thread *td = curthread;
2830 struct proc *p = td->td_proc;
2835 struct vattr *vap = &vat;
2838 struct filedesc *fdp = p->p_fd;
2839 int fmode, mode, error, type;
2844 * Must be super user
2850 fmode = FFLAGS(SCARG(uap, flags));
2851 /* why not allow a non-read/write open for our lockd? */
2852 if (((fmode & (FREAD | FWRITE)) == 0) || (fmode & O_CREAT))
2854 error = copyin(SCARG(uap,u_fhp), &fhp, sizeof(fhp));
2857 /* find the mount point */
2858 mp = vfs_getvfs(&fhp.fh_fsid);
2861 /* now give me my vnode, it gets returned to me locked */
2862 error = VFS_FHTOVP(mp, &fhp.fh_fid, &vp);
2866 * from now on we have to make sure not
2867 * to forget about the vnode
2868 * any error that causes an abort must vput(vp)
2869 * just set error = err and 'goto bad;'.
2875 if (vp->v_type == VLNK) {
2879 if (vp->v_type == VSOCK) {
2884 if (fmode & (FWRITE | O_TRUNC)) {
2885 if (vp->v_type == VDIR) {
2889 error = vn_writechk(vp);
2897 error = VOP_ACCESS(vp, mode, p->p_ucred, td);
2901 if (fmode & O_TRUNC) {
2902 VOP_UNLOCK(vp, 0, td); /* XXX */
2903 VOP_LEASE(vp, td, p->p_ucred, LEASE_WRITE);
2904 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY, td); /* XXX */
2907 error = VOP_SETATTR(vp, vap, p->p_ucred, td);
2911 error = VOP_OPEN(vp, fmode, p->p_ucred, td);
2915 * Make sure that a VM object is created for VMIO support.
2917 if (vn_canvmio(vp) == TRUE) {
2918 if ((error = vfs_object_create(vp, td)) != 0)
2925 * end of vn_open code
2928 if ((error = falloc(p, &nfp, &indx)) != 0) {
2936 * hold an extra reference to avoid having fp ripped out
2937 * from under us while we block in the lock op.
2940 nfp->f_data = (caddr_t)vp;
2941 nfp->f_flag = fmode & FMASK;
2942 nfp->f_ops = &vnops;
2943 nfp->f_type = DTYPE_VNODE;
2944 if (fmode & (O_EXLOCK | O_SHLOCK)) {
2945 lf.l_whence = SEEK_SET;
2948 if (fmode & O_EXLOCK)
2949 lf.l_type = F_WRLCK;
2951 lf.l_type = F_RDLCK;
2953 if ((fmode & FNONBLOCK) == 0)
2955 VOP_UNLOCK(vp, 0, td);
2956 if ((error = VOP_ADVLOCK(vp, (caddr_t)fp, F_SETLK, &lf, type)) != 0) {
2958 * lock request failed. Normally close the descriptor
2959 * but handle the case where someone might have dup()d
2960 * or close()d it when we weren't looking.
2962 if (fdp->fd_ofiles[indx] == fp) {
2963 fdp->fd_ofiles[indx] = NULL;
2968 * release our private reference.
2973 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY, td);
2974 fp->f_flag |= FHASLOCK;
2976 if ((vp->v_type == VREG) && (VOP_GETVOBJECT(vp, NULL) != 0))
2977 vfs_object_create(vp, td);
2979 VOP_UNLOCK(vp, 0, td);
2981 p->p_retval[0] = indx;
2990 * fhstat_args(struct fhandle *u_fhp, struct stat *sb)
2993 fhstat(struct fhstat_args *uap)
2995 struct thread *td = curthread;
3003 * Must be super user
3009 error = copyin(SCARG(uap, u_fhp), &fh, sizeof(fhandle_t));
3013 if ((mp = vfs_getvfs(&fh.fh_fsid)) == NULL)
3015 if ((error = VFS_FHTOVP(mp, &fh.fh_fid, &vp)))
3017 error = vn_stat(vp, &sb, td);
3021 error = copyout(&sb, SCARG(uap, sb), sizeof(sb));
3026 * fhstatfs_args(struct fhandle *u_fhp, struct statfs *buf)
3029 fhstatfs(struct fhstatfs_args *uap)
3031 struct thread *td = curthread;
3040 * Must be super user
3042 if ((error = suser(td)))
3045 if ((error = copyin(SCARG(uap, u_fhp), &fh, sizeof(fhandle_t))) != 0)
3048 if ((mp = vfs_getvfs(&fh.fh_fsid)) == NULL)
3050 if ((error = VFS_FHTOVP(mp, &fh.fh_fid, &vp)))
3055 if ((error = VFS_STATFS(mp, sp, td)) != 0)
3057 sp->f_flags = mp->mnt_flag & MNT_VISFLAGMASK;
3059 bcopy((caddr_t)sp, (caddr_t)&sb, sizeof(sb));
3060 sb.f_fsid.val[0] = sb.f_fsid.val[1] = 0;
3063 return (copyout(sp, SCARG(uap, buf), sizeof(*sp)));
3067 * Syscall to push extended attribute configuration information into the
3068 * VFS. Accepts a path, which it converts to a mountpoint, as well as
3069 * a command (int cmd), and attribute name and misc data. For now, the
3070 * attribute name is left in userspace for consumption by the VFS_op.
3071 * It will probably be changed to be copied into sysspace by the
3072 * syscall in the future, once issues with various consumers of the
3073 * attribute code have raised their hands.
3075 * Currently this is used only by UFS Extended Attributes.
3078 extattrctl(struct extattrctl_args *uap)
3080 struct thread *td = curthread;
3081 struct nameidata nd;
3085 NDINIT(&nd, LOOKUP, FOLLOW, UIO_USERSPACE, SCARG(uap, path), td);
3086 if ((error = namei(&nd)) != 0)
3088 mp = nd.ni_vp->v_mount;
3090 return (VFS_EXTATTRCTL(mp, SCARG(uap, cmd), SCARG(uap, attrname),
3091 SCARG(uap, arg), td));
3095 * Syscall to set a named extended attribute on a file or directory.
3096 * Accepts attribute name, and a uio structure pointing to the data to set.
3097 * The uio is consumed in the style of writev(). The real work happens
3098 * in VOP_SETEXTATTR().
3101 extattr_set_file(struct extattr_set_file_args *uap)
3103 struct thread *td = curthread;
3104 struct proc *p = td->td_proc;
3105 struct nameidata nd;
3107 struct iovec *iov, *needfree = NULL, aiov[UIO_SMALLIOV];
3108 char attrname[EXTATTR_MAXNAMELEN];
3112 error = copyin(SCARG(uap, attrname), attrname, EXTATTR_MAXNAMELEN);
3115 NDINIT(&nd, LOOKUP, FOLLOW | LOCKLEAF, UIO_USERSPACE,
3116 SCARG(uap, path), td);
3117 if ((error = namei(&nd)) != 0)
3119 iovlen = uap->iovcnt * sizeof(struct iovec);
3120 if (uap->iovcnt > UIO_SMALLIOV) {
3121 if (uap->iovcnt > UIO_MAXIOV) {
3125 MALLOC(iov, struct iovec *, iovlen, M_IOV, M_WAITOK);
3130 auio.uio_iovcnt = uap->iovcnt;
3131 auio.uio_rw = UIO_WRITE;
3132 auio.uio_segflg = UIO_USERSPACE;
3134 auio.uio_offset = 0;
3135 if ((error = copyin((caddr_t)uap->iovp, (caddr_t)iov, iovlen)))
3138 for (i = 0; i < uap->iovcnt; i++) {
3139 if (iov->iov_len > INT_MAX - auio.uio_resid) {
3143 auio.uio_resid += iov->iov_len;
3146 cnt = auio.uio_resid;
3147 error = VOP_SETEXTATTR(nd.ni_vp, attrname, &auio, p->p_ucred, td);
3148 cnt -= auio.uio_resid;
3149 p->p_retval[0] = cnt;
3152 FREE(needfree, M_IOV);
3158 * Syscall to get a named extended attribute on a file or directory.
3159 * Accepts attribute name, and a uio structure pointing to a buffer for the
3160 * data. The uio is consumed in the style of readv(). The real work
3161 * happens in VOP_GETEXTATTR();
3164 extattr_get_file(struct extattr_get_file_args *uap)
3166 struct thread *td = curthread;
3167 struct proc *p = td->td_proc;
3168 struct nameidata nd;
3170 struct iovec *iov, *needfree, aiov[UIO_SMALLIOV];
3171 char attrname[EXTATTR_MAXNAMELEN];
3175 error = copyin(SCARG(uap, attrname), attrname, EXTATTR_MAXNAMELEN);
3178 NDINIT(&nd, LOOKUP, FOLLOW | LOCKLEAF, UIO_USERSPACE,
3179 SCARG(uap, path), td);
3180 if ((error = namei(&nd)) != 0)
3182 iovlen = uap->iovcnt * sizeof (struct iovec);
3183 if (uap->iovcnt > UIO_SMALLIOV) {
3184 if (uap->iovcnt > UIO_MAXIOV) {
3188 MALLOC(iov, struct iovec *, iovlen, M_IOV, M_WAITOK);
3195 auio.uio_iovcnt = uap->iovcnt;
3196 auio.uio_rw = UIO_READ;
3197 auio.uio_segflg = UIO_USERSPACE;
3199 auio.uio_offset = 0;
3200 if ((error = copyin((caddr_t)uap->iovp, (caddr_t)iov, iovlen)))
3203 for (i = 0; i < uap->iovcnt; i++) {
3204 if (iov->iov_len > INT_MAX - auio.uio_resid) {
3208 auio.uio_resid += iov->iov_len;
3211 cnt = auio.uio_resid;
3212 error = VOP_GETEXTATTR(nd.ni_vp, attrname, &auio, p->p_ucred, td);
3213 cnt -= auio.uio_resid;
3214 p->p_retval[0] = cnt;
3217 FREE(needfree, M_IOV);
3223 * Syscall to delete a named extended attribute from a file or directory.
3224 * Accepts attribute name. The real work happens in VOP_SETEXTATTR().
3227 extattr_delete_file(struct extattr_delete_file_args *uap)
3229 struct thread *td = curthread;
3230 struct proc *p = td->td_proc;
3231 struct nameidata nd;
3232 char attrname[EXTATTR_MAXNAMELEN];
3235 error = copyin(SCARG(uap, attrname), attrname, EXTATTR_MAXNAMELEN);
3238 NDINIT(&nd, LOOKUP, FOLLOW | LOCKLEAF, UIO_USERSPACE,
3239 SCARG(uap, path), td);
3240 if ((error = namei(&nd)) != 0)
3242 error = VOP_SETEXTATTR(nd.ni_vp, attrname, NULL, p->p_ucred, td);