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.10 2003/07/24 01:41:25 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.
98 * mount_args(char *type, char *path, int flags, caddr_t data)
102 mount(struct mount_args *uap)
104 struct thread *td = curthread;
105 struct proc *p = td->td_proc;
108 struct vfsconf *vfsp;
109 int error, flag = 0, flag2 = 0;
115 char fstypename[MFSNAMELEN];
117 if (usermount == 0 && (error = suser(td)))
120 * Do not allow NFS export by non-root users.
122 if (SCARG(uap, flags) & MNT_EXPORTED) {
128 * Silently enforce MNT_NOSUID and MNT_NODEV for non-root users
131 SCARG(uap, flags) |= MNT_NOSUID | MNT_NODEV;
133 * Get vnode to be covered
135 NDINIT(&nd, LOOKUP, FOLLOW | LOCKLEAF, UIO_USERSPACE,
136 SCARG(uap, path), td);
137 if ((error = namei(&nd)) != 0)
139 NDFREE(&nd, NDF_ONLY_PNBUF);
141 if (SCARG(uap, flags) & MNT_UPDATE) {
142 if ((vp->v_flag & VROOT) == 0) {
148 flag2 = mp->mnt_kern_flag;
150 * We only allow the filesystem to be reloaded if it
151 * is currently mounted read-only.
153 if ((SCARG(uap, flags) & MNT_RELOAD) &&
154 ((mp->mnt_flag & MNT_RDONLY) == 0)) {
156 return (EOPNOTSUPP); /* Needs translation */
159 * Only root, or the user that did the original mount is
160 * permitted to update it.
162 if (mp->mnt_stat.f_owner != p->p_ucred->cr_uid &&
163 (error = suser(td))) {
167 if (vfs_busy(mp, LK_NOWAIT, 0, td)) {
171 lwkt_gettoken(&vp->v_interlock);
172 if ((vp->v_flag & VMOUNT) != 0 ||
173 vp->v_mountedhere != NULL) {
174 lwkt_reltoken(&vp->v_interlock);
179 vp->v_flag |= VMOUNT;
180 lwkt_reltoken(&vp->v_interlock);
182 SCARG(uap, flags) & (MNT_RELOAD | MNT_FORCE | MNT_UPDATE);
183 VOP_UNLOCK(vp, 0, td);
187 * If the user is not root, ensure that they own the directory
188 * onto which we are attempting to mount.
190 if ((error = VOP_GETATTR(vp, &va, td)) ||
191 (va.va_uid != p->p_ucred->cr_uid &&
192 (error = suser(td)))) {
196 if ((error = vinvalbuf(vp, V_SAVE, td, 0, 0)) != 0) {
200 if (vp->v_type != VDIR) {
206 * Historically filesystem types were identified by number. If we
207 * get an integer for the filesystem type instead of a string, we
208 * check to see if it matches one of the historic filesystem types.
210 fstypenum = (uintptr_t)SCARG(uap, type);
211 if (fstypenum < maxvfsconf) {
212 for (vfsp = vfsconf; vfsp; vfsp = vfsp->vfc_next)
213 if (vfsp->vfc_typenum == fstypenum)
219 strncpy(fstypename, vfsp->vfc_name, MFSNAMELEN);
221 #endif /* COMPAT_43 */
222 if ((error = copyinstr(SCARG(uap, type), fstypename, MFSNAMELEN, NULL)) != 0) {
226 for (vfsp = vfsconf; vfsp; vfsp = vfsp->vfc_next)
227 if (!strcmp(vfsp->vfc_name, fstypename))
232 /* Only load modules for root (very important!) */
233 if ((error = suser(td)) != 0) {
237 error = linker_load_file(fstypename, &lf);
238 if (error || lf == NULL) {
245 /* lookup again, see if the VFS was loaded */
246 for (vfsp = vfsconf; vfsp; vfsp = vfsp->vfc_next)
247 if (!strcmp(vfsp->vfc_name, fstypename))
251 linker_file_unload(lf);
256 lwkt_gettoken(&vp->v_interlock);
257 if ((vp->v_flag & VMOUNT) != 0 ||
258 vp->v_mountedhere != NULL) {
259 lwkt_reltoken(&vp->v_interlock);
263 vp->v_flag |= VMOUNT;
264 lwkt_reltoken(&vp->v_interlock);
267 * Allocate and initialize the filesystem.
269 mp = malloc(sizeof(struct mount), M_MOUNT, M_WAITOK);
270 bzero((char *)mp, (u_long)sizeof(struct mount));
271 TAILQ_INIT(&mp->mnt_nvnodelist);
272 TAILQ_INIT(&mp->mnt_reservedvnlist);
273 mp->mnt_nvnodelistsize = 0;
274 lockinit(&mp->mnt_lock, 0, "vfslock", 0, LK_NOPAUSE);
275 (void)vfs_busy(mp, LK_NOWAIT, 0, td);
276 mp->mnt_op = vfsp->vfc_vfsops;
278 vfsp->vfc_refcount++;
279 mp->mnt_stat.f_type = vfsp->vfc_typenum;
280 mp->mnt_flag |= vfsp->vfc_flags & MNT_VISFLAGMASK;
281 strncpy(mp->mnt_stat.f_fstypename, vfsp->vfc_name, MFSNAMELEN);
282 mp->mnt_vnodecovered = vp;
283 mp->mnt_stat.f_owner = p->p_ucred->cr_uid;
284 mp->mnt_iosize_max = DFLTPHYS;
285 VOP_UNLOCK(vp, 0, td);
288 * Set the mount level flags.
290 if (SCARG(uap, flags) & MNT_RDONLY)
291 mp->mnt_flag |= MNT_RDONLY;
292 else if (mp->mnt_flag & MNT_RDONLY)
293 mp->mnt_kern_flag |= MNTK_WANTRDWR;
294 mp->mnt_flag &=~ (MNT_NOSUID | MNT_NOEXEC | MNT_NODEV |
295 MNT_SYNCHRONOUS | MNT_UNION | MNT_ASYNC | MNT_NOATIME |
296 MNT_NOSYMFOLLOW | MNT_IGNORE |
297 MNT_NOCLUSTERR | MNT_NOCLUSTERW | MNT_SUIDDIR);
298 mp->mnt_flag |= SCARG(uap, flags) & (MNT_NOSUID | MNT_NOEXEC |
299 MNT_NODEV | MNT_SYNCHRONOUS | MNT_UNION | MNT_ASYNC | MNT_FORCE |
300 MNT_NOSYMFOLLOW | MNT_IGNORE |
301 MNT_NOATIME | MNT_NOCLUSTERR | MNT_NOCLUSTERW | MNT_SUIDDIR);
303 * Mount the filesystem.
304 * XXX The final recipients of VFS_MOUNT just overwrite the ndp they
305 * get. No freeing of cn_pnbuf.
307 error = VFS_MOUNT(mp, SCARG(uap, path), SCARG(uap, data), &nd, td);
308 if (mp->mnt_flag & MNT_UPDATE) {
309 if (mp->mnt_kern_flag & MNTK_WANTRDWR)
310 mp->mnt_flag &= ~MNT_RDONLY;
311 mp->mnt_flag &=~ (MNT_UPDATE | MNT_RELOAD | MNT_FORCE);
312 mp->mnt_kern_flag &=~ MNTK_WANTRDWR;
315 mp->mnt_kern_flag = flag2;
317 if ((mp->mnt_flag & MNT_RDONLY) == 0) {
318 if (mp->mnt_syncer == NULL)
319 error = vfs_allocate_syncvnode(mp);
321 if (mp->mnt_syncer != NULL)
322 vrele(mp->mnt_syncer);
323 mp->mnt_syncer = NULL;
326 lwkt_gettoken(&vp->v_interlock);
327 vp->v_flag &= ~VMOUNT;
328 lwkt_reltoken(&vp->v_interlock);
332 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY, td);
334 * Put the new filesystem on the mount list after root.
338 lwkt_gettoken(&vp->v_interlock);
339 vp->v_flag &= ~VMOUNT;
340 vp->v_mountedhere = mp;
341 lwkt_reltoken(&vp->v_interlock);
342 lwkt_gettoken(&mountlist_token);
343 TAILQ_INSERT_TAIL(&mountlist, mp, mnt_list);
344 lwkt_reltoken(&mountlist_token);
346 VOP_UNLOCK(vp, 0, td);
347 if ((mp->mnt_flag & MNT_RDONLY) == 0)
348 error = vfs_allocate_syncvnode(mp);
350 if ((error = VFS_START(mp, 0, td)) != 0)
353 lwkt_gettoken(&vp->v_interlock);
354 vp->v_flag &= ~VMOUNT;
355 lwkt_reltoken(&vp->v_interlock);
356 mp->mnt_vfc->vfc_refcount--;
358 free((caddr_t)mp, M_MOUNT);
365 * Scan all active processes to see if any of them have a current
366 * or root directory onto which the new filesystem has just been
367 * mounted. If so, replace them with the new mount point.
370 checkdirs(struct vnode *olddp)
372 struct filedesc *fdp;
376 if (olddp->v_usecount == 1)
378 if (VFS_ROOT(olddp->v_mountedhere, &newdp))
379 panic("mount: lost mount");
380 LIST_FOREACH(p, &allproc, p_list) {
382 if (fdp->fd_cdir == olddp) {
385 fdp->fd_cdir = newdp;
387 if (fdp->fd_rdir == olddp) {
390 fdp->fd_rdir = newdp;
393 if (rootvnode == olddp) {
402 * Unmount a file system.
404 * Note: unmount takes a path to the vnode mounted on as argument,
405 * not special file (as before).
408 * umount_args(char *path, int flags)
412 unmount(struct unmount_args *uap)
414 struct thread *td = curthread;
415 struct proc *p = td->td_proc;
422 NDINIT(&nd, LOOKUP, FOLLOW | LOCKLEAF, UIO_USERSPACE,
423 SCARG(uap, path), td);
424 if ((error = namei(&nd)) != 0)
427 NDFREE(&nd, NDF_ONLY_PNBUF);
431 * Only root, or the user that did the original mount is
432 * permitted to unmount this filesystem.
434 if ((mp->mnt_stat.f_owner != p->p_ucred->cr_uid) &&
435 (error = suser(td))) {
441 * Don't allow unmounting the root file system.
443 if (mp->mnt_flag & MNT_ROOTFS) {
449 * Must be the root of the filesystem
451 if ((vp->v_flag & VROOT) == 0) {
456 return (dounmount(mp, SCARG(uap, flags), td));
460 * Do the actual file system unmount.
463 dounmount(struct mount *mp, int flags, struct thread *td)
465 struct vnode *coveredvp;
469 lwkt_gettoken(&mountlist_token);
470 if (mp->mnt_kern_flag & MNTK_UNMOUNT) {
471 lwkt_reltoken(&mountlist_token);
474 mp->mnt_kern_flag |= MNTK_UNMOUNT;
475 /* Allow filesystems to detect that a forced unmount is in progress. */
476 if (flags & MNT_FORCE)
477 mp->mnt_kern_flag |= MNTK_UNMOUNTF;
478 error = lockmgr(&mp->mnt_lock, LK_DRAIN | LK_INTERLOCK |
479 ((flags & MNT_FORCE) ? 0 : LK_NOWAIT), &mountlist_token, td);
481 mp->mnt_kern_flag &= ~(MNTK_UNMOUNT | MNTK_UNMOUNTF);
482 if (mp->mnt_kern_flag & MNTK_MWAIT)
487 if (mp->mnt_flag & MNT_EXPUBLIC)
488 vfs_setpublicfs(NULL, NULL, NULL);
490 vfs_msync(mp, MNT_WAIT);
491 async_flag = mp->mnt_flag & MNT_ASYNC;
492 mp->mnt_flag &=~ MNT_ASYNC;
493 cache_purgevfs(mp); /* remove cache entries for this file sys */
494 if (mp->mnt_syncer != NULL)
495 vrele(mp->mnt_syncer);
496 if (((mp->mnt_flag & MNT_RDONLY) ||
497 (error = VFS_SYNC(mp, MNT_WAIT, td)) == 0) ||
499 error = VFS_UNMOUNT(mp, flags, td);
500 lwkt_gettoken(&mountlist_token);
502 if ((mp->mnt_flag & MNT_RDONLY) == 0 && mp->mnt_syncer == NULL)
503 (void) vfs_allocate_syncvnode(mp);
504 mp->mnt_kern_flag &= ~(MNTK_UNMOUNT | MNTK_UNMOUNTF);
505 mp->mnt_flag |= async_flag;
506 lockmgr(&mp->mnt_lock, LK_RELEASE | LK_INTERLOCK | LK_REENABLE,
507 &mountlist_token, td);
508 if (mp->mnt_kern_flag & MNTK_MWAIT)
512 TAILQ_REMOVE(&mountlist, mp, mnt_list);
513 if ((coveredvp = mp->mnt_vnodecovered) != NULLVP) {
514 coveredvp->v_mountedhere = (struct mount *)0;
517 mp->mnt_vfc->vfc_refcount--;
518 if (!TAILQ_EMPTY(&mp->mnt_nvnodelist))
519 panic("unmount: dangling vnode");
520 lockmgr(&mp->mnt_lock, LK_RELEASE | LK_INTERLOCK, &mountlist_token, td);
521 if (mp->mnt_kern_flag & MNTK_MWAIT)
523 free((caddr_t)mp, M_MOUNT);
528 * Sync each mounted filesystem.
532 static int syncprt = 0;
533 SYSCTL_INT(_debug, OID_AUTO, syncprt, CTLFLAG_RW, &syncprt, 0, "");
538 sync(struct sync_args *uap)
540 struct thread *td = curthread;
541 struct mount *mp, *nmp;
544 lwkt_gettoken(&mountlist_token);
545 for (mp = TAILQ_FIRST(&mountlist); mp != NULL; mp = nmp) {
546 if (vfs_busy(mp, LK_NOWAIT, &mountlist_token, td)) {
547 nmp = TAILQ_NEXT(mp, mnt_list);
550 if ((mp->mnt_flag & MNT_RDONLY) == 0) {
551 asyncflag = mp->mnt_flag & MNT_ASYNC;
552 mp->mnt_flag &= ~MNT_ASYNC;
553 vfs_msync(mp, MNT_NOWAIT);
554 VFS_SYNC(mp, MNT_NOWAIT, td);
555 mp->mnt_flag |= asyncflag;
557 lwkt_gettoken(&mountlist_token);
558 nmp = TAILQ_NEXT(mp, mnt_list);
561 lwkt_reltoken(&mountlist_token);
564 * XXX don't call vfs_bufstats() yet because that routine
565 * was not imported in the Lite2 merge.
570 #endif /* DIAGNOSTIC */
575 /* XXX PRISON: could be per prison flag */
576 static int prison_quotas;
578 SYSCTL_INT(_kern_prison, OID_AUTO, quotas, CTLFLAG_RW, &prison_quotas, 0, "");
582 * quotactl_args(char *path, int fcmd, int uid, caddr_t arg)
584 * Change filesystem quotas.
588 quotactl(struct quotactl_args *uap)
590 struct thread *td = curthread;
591 struct proc *p = td->td_proc;
597 if (p->p_ucred->cr_prison && !prison_quotas)
599 NDINIT(&nd, LOOKUP, FOLLOW, UIO_USERSPACE, SCARG(uap, path), td);
600 if ((error = namei(&nd)) != 0)
602 mp = nd.ni_vp->v_mount;
603 NDFREE(&nd, NDF_ONLY_PNBUF);
605 return (VFS_QUOTACTL(mp, SCARG(uap, cmd), SCARG(uap, uid),
606 SCARG(uap, arg), td));
610 * statfs_args(char *path, struct statfs *buf)
612 * Get filesystem statistics.
616 statfs(struct statfs_args *uap)
618 struct thread *td = curthread;
625 NDINIT(&nd, LOOKUP, FOLLOW, UIO_USERSPACE, SCARG(uap, path), td);
626 if ((error = namei(&nd)) != 0)
628 mp = nd.ni_vp->v_mount;
630 NDFREE(&nd, NDF_ONLY_PNBUF);
632 error = VFS_STATFS(mp, sp, td);
635 sp->f_flags = mp->mnt_flag & MNT_VISFLAGMASK;
637 bcopy((caddr_t)sp, (caddr_t)&sb, sizeof(sb));
638 sb.f_fsid.val[0] = sb.f_fsid.val[1] = 0;
641 return (copyout((caddr_t)sp, (caddr_t)SCARG(uap, buf), sizeof(*sp)));
645 * fstatfs_args(int fd, struct statfs *buf)
647 * Get filesystem statistics.
651 fstatfs(struct fstatfs_args *uap)
653 struct thread *td = curthread;
654 struct proc *p = td->td_proc;
657 register struct statfs *sp;
662 if ((error = getvnode(p->p_fd, SCARG(uap, fd), &fp)) != 0)
664 mp = ((struct vnode *)fp->f_data)->v_mount;
668 error = VFS_STATFS(mp, sp, td);
671 sp->f_flags = mp->mnt_flag & MNT_VISFLAGMASK;
673 bcopy((caddr_t)sp, (caddr_t)&sb, sizeof(sb));
674 sb.f_fsid.val[0] = sb.f_fsid.val[1] = 0;
677 return (copyout((caddr_t)sp, (caddr_t)SCARG(uap, buf), sizeof(*sp)));
681 * getfsstat_args(struct statfs *buf, long bufsize, int flags)
683 * Get statistics on all filesystems.
687 getfsstat(struct getfsstat_args *uap)
689 struct thread *td = curthread;
690 struct proc *p = td->td_proc;
691 struct mount *mp, *nmp;
694 long count, maxcount, error;
696 maxcount = SCARG(uap, bufsize) / sizeof(struct statfs);
697 sfsp = (caddr_t)SCARG(uap, buf);
699 lwkt_gettoken(&mountlist_token);
700 for (mp = TAILQ_FIRST(&mountlist); mp != NULL; mp = nmp) {
701 if (vfs_busy(mp, LK_NOWAIT, &mountlist_token, td)) {
702 nmp = TAILQ_NEXT(mp, mnt_list);
705 if (sfsp && count < maxcount) {
708 * If MNT_NOWAIT or MNT_LAZY is specified, do not
709 * refresh the fsstat cache. MNT_NOWAIT or MNT_LAZY
710 * overrides MNT_WAIT.
712 if (((SCARG(uap, flags) & (MNT_LAZY|MNT_NOWAIT)) == 0 ||
713 (SCARG(uap, flags) & MNT_WAIT)) &&
714 (error = VFS_STATFS(mp, sp, td))) {
715 lwkt_gettoken(&mountlist_token);
716 nmp = TAILQ_NEXT(mp, mnt_list);
720 sp->f_flags = mp->mnt_flag & MNT_VISFLAGMASK;
721 error = copyout((caddr_t)sp, sfsp, sizeof(*sp));
729 lwkt_gettoken(&mountlist_token);
730 nmp = TAILQ_NEXT(mp, mnt_list);
733 lwkt_reltoken(&mountlist_token);
734 if (sfsp && count > maxcount)
735 p->p_retval[0] = maxcount;
737 p->p_retval[0] = count;
742 * fchdir_args(int fd)
744 * Change current working directory to a given file descriptor.
748 fchdir(struct fchdir_args *uap)
750 struct thread *td = curthread;
751 struct proc *p = td->td_proc;
752 struct filedesc *fdp = p->p_fd;
753 struct vnode *vp, *tdp;
758 if ((error = getvnode(fdp, SCARG(uap, fd), &fp)) != 0)
760 vp = (struct vnode *)fp->f_data;
762 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY, td);
763 if (vp->v_type != VDIR)
766 error = VOP_ACCESS(vp, VEXEC, p->p_ucred, td);
767 while (!error && (mp = vp->v_mountedhere) != NULL) {
768 if (vfs_busy(mp, 0, 0, td))
770 error = VFS_ROOT(mp, &tdp);
781 VOP_UNLOCK(vp, 0, td);
788 * chdir_args(char *path)
790 * Change current working directory (``.'').
794 chdir(struct chdir_args *uap)
796 struct thread *td = curthread;
797 struct proc *p = td->td_proc;
798 struct filedesc *fdp = p->p_fd;
802 NDINIT(&nd, LOOKUP, FOLLOW | LOCKLEAF, UIO_USERSPACE,
803 SCARG(uap, path), td);
804 if ((error = change_dir(&nd, td)) != 0)
806 NDFREE(&nd, NDF_ONLY_PNBUF);
808 fdp->fd_cdir = nd.ni_vp;
813 * Helper function for raised chroot(2) security function: Refuse if
814 * any filedescriptors are open directories.
817 chroot_refuse_vdir_fds(fdp)
818 struct filedesc *fdp;
825 for (fd = 0; fd < fdp->fd_nfiles ; fd++) {
826 error = getvnode(fdp, fd, &fp);
829 vp = (struct vnode *)fp->f_data;
830 if (vp->v_type != VDIR)
838 * This sysctl determines if we will allow a process to chroot(2) if it
839 * has a directory open:
840 * 0: disallowed for all processes.
841 * 1: allowed for processes that were not already chroot(2)'ed.
842 * 2: allowed for all processes.
845 static int chroot_allow_open_directories = 1;
847 SYSCTL_INT(_kern, OID_AUTO, chroot_allow_open_directories, CTLFLAG_RW,
848 &chroot_allow_open_directories, 0, "");
851 * chroot_args(char *path)
853 * Change notion of root (``/'') directory.
857 chroot(struct chroot_args *uap)
859 struct thread *td = curthread;
860 struct proc *p = td->td_proc;
861 struct filedesc *fdp = p->p_fd;
866 error = suser_cred(p->p_ucred, PRISON_ROOT);
869 if (chroot_allow_open_directories == 0 ||
870 (chroot_allow_open_directories == 1 && fdp->fd_rdir != rootvnode))
871 error = chroot_refuse_vdir_fds(fdp);
874 NDINIT(&nd, LOOKUP, FOLLOW | LOCKLEAF, UIO_USERSPACE,
875 SCARG(uap, path), td);
876 if ((error = change_dir(&nd, td)) != 0)
878 NDFREE(&nd, NDF_ONLY_PNBUF);
880 fdp->fd_rdir = nd.ni_vp;
882 fdp->fd_jdir = nd.ni_vp;
889 * Common routine for chroot and chdir.
892 change_dir(struct nameidata *ndp, struct thread *td)
901 if (vp->v_type != VDIR)
904 error = VOP_ACCESS(vp, VEXEC, ndp->ni_cnd.cn_cred, td);
908 VOP_UNLOCK(vp, 0, td);
913 * open_args(char *path, int flags, int mode)
915 * Check permissions, allocate an open file structure,
916 * and call the device open routine if any.
919 open(struct open_args *uap)
921 struct thread *td = curthread;
922 struct proc *p = td->td_proc;
923 struct filedesc *fdp = p->p_fd;
926 int cmode, flags, oflags;
928 int type, indx, error;
932 oflags = SCARG(uap, flags);
933 if ((oflags & O_ACCMODE) == O_ACCMODE)
935 flags = FFLAGS(oflags);
936 error = falloc(p, &nfp, &indx);
940 cmode = ((SCARG(uap, mode) &~ fdp->fd_cmask) & ALLPERMS) &~ S_ISTXT;
941 NDINIT(&nd, LOOKUP, FOLLOW, UIO_USERSPACE, SCARG(uap, path), td);
942 p->p_dupfd = -indx - 1; /* XXX check for fdopen */
944 * Bump the ref count to prevent another process from closing
945 * the descriptor while we are blocked in vn_open()
948 error = vn_open(&nd, flags, cmode);
951 * release our own reference
956 * handle special fdopen() case. bleh. dupfdopen() is
957 * responsible for dropping the old contents of ofiles[indx]
960 if ((error == ENODEV || error == ENXIO) &&
961 p->p_dupfd >= 0 && /* XXX from fdopen */
963 dupfdopen(fdp, indx, p->p_dupfd, flags, error)) == 0) {
964 p->p_retval[0] = indx;
968 * Clean up the descriptor, but only if another thread hadn't
969 * replaced or closed it.
971 if (fdp->fd_ofiles[indx] == fp) {
972 fdp->fd_ofiles[indx] = NULL;
976 if (error == ERESTART)
981 NDFREE(&nd, NDF_ONLY_PNBUF);
985 * There should be 2 references on the file, one from the descriptor
986 * table, and one for us.
988 * Handle the case where someone closed the file (via its file
989 * descriptor) while we were blocked. The end result should look
990 * like opening the file succeeded but it was immediately closed.
992 if (fp->f_count == 1) {
993 KASSERT(fdp->fd_ofiles[indx] != fp,
994 ("Open file descriptor lost all refs"));
995 VOP_UNLOCK(vp, 0, td);
996 vn_close(vp, flags & FMASK, td);
998 p->p_retval[0] = indx;
1002 fp->f_data = (caddr_t)vp;
1003 fp->f_flag = flags & FMASK;
1005 fp->f_type = (vp->v_type == VFIFO ? DTYPE_FIFO : DTYPE_VNODE);
1006 if (flags & (O_EXLOCK | O_SHLOCK)) {
1007 lf.l_whence = SEEK_SET;
1010 if (flags & O_EXLOCK)
1011 lf.l_type = F_WRLCK;
1013 lf.l_type = F_RDLCK;
1015 if ((flags & FNONBLOCK) == 0)
1017 VOP_UNLOCK(vp, 0, td);
1018 if ((error = VOP_ADVLOCK(vp, (caddr_t)fp, F_SETLK, &lf, type)) != 0) {
1020 * lock request failed. Normally close the descriptor
1021 * but handle the case where someone might have dup()d
1022 * it when we weren't looking. One reference is
1023 * owned by the descriptor array, the other by us.
1025 if (fdp->fd_ofiles[indx] == fp) {
1026 fdp->fd_ofiles[indx] = NULL;
1032 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY, td);
1033 fp->f_flag |= FHASLOCK;
1035 /* assert that vn_open created a backing object if one is needed */
1036 KASSERT(!vn_canvmio(vp) || VOP_GETVOBJECT(vp, NULL) == 0,
1037 ("open: vmio vnode has no backing object after vn_open"));
1038 VOP_UNLOCK(vp, 0, td);
1041 * release our private reference, leaving the one associated with the
1042 * descriptor table intact.
1045 p->p_retval[0] = indx;
1051 * ocreat(char *path, int mode)
1056 ocreat(struct ocreat_args *uap)
1058 struct open_args /* {
1059 syscallarg(char *) path;
1060 syscallarg(int) flags;
1061 syscallarg(int) mode;
1064 SCARG(&nuap, path) = SCARG(uap, path);
1065 SCARG(&nuap, mode) = SCARG(uap, mode);
1066 SCARG(&nuap, flags) = O_WRONLY | O_CREAT | O_TRUNC;
1067 return (open(&nuap));
1069 #endif /* COMPAT_43 */
1072 * mknod_args(char *path, int mode, int dev)
1074 * Create a special file.
1078 mknod(struct mknod_args *uap)
1080 struct thread *td = curthread;
1081 struct proc *p = td->td_proc;
1086 struct nameidata nd;
1090 switch (SCARG(uap, mode) & S_IFMT) {
1096 error = suser_cred(p->p_ucred, PRISON_ROOT);
1102 NDINIT(&nd, CREATE, LOCKPARENT, UIO_USERSPACE, SCARG(uap, path), td);
1103 if ((error = namei(&nd)) != 0)
1110 vattr.va_mode = (SCARG(uap, mode) & ALLPERMS) &~ p->p_fd->fd_cmask;
1111 vattr.va_rdev = SCARG(uap, dev);
1114 switch (SCARG(uap, mode) & S_IFMT) {
1115 case S_IFMT: /* used by badsect to flag bad sectors */
1116 vattr.va_type = VBAD;
1119 vattr.va_type = VCHR;
1122 vattr.va_type = VBLK;
1133 VOP_LEASE(nd.ni_dvp, td, p->p_ucred, LEASE_WRITE);
1135 error = VOP_WHITEOUT(nd.ni_dvp, &nd.ni_cnd, CREATE);
1137 error = VOP_MKNOD(nd.ni_dvp, &nd.ni_vp,
1138 &nd.ni_cnd, &vattr);
1142 NDFREE(&nd, NDF_ONLY_PNBUF);
1145 NDFREE(&nd, NDF_ONLY_PNBUF);
1146 if (nd.ni_dvp == vp)
1153 ASSERT_VOP_UNLOCKED(nd.ni_dvp, "mknod");
1154 ASSERT_VOP_UNLOCKED(nd.ni_vp, "mknod");
1159 * mkfifo_args(char *path, int mode)
1161 * Create a named pipe.
1165 mkfifo(struct mkfifo_args *uap)
1167 struct thread *td = curthread;
1168 struct proc *p = td->td_proc;
1171 struct nameidata nd;
1174 NDINIT(&nd, CREATE, LOCKPARENT, UIO_USERSPACE, SCARG(uap, path), td);
1175 if ((error = namei(&nd)) != 0)
1177 if (nd.ni_vp != NULL) {
1178 NDFREE(&nd, NDF_ONLY_PNBUF);
1179 if (nd.ni_dvp == nd.ni_vp)
1187 vattr.va_type = VFIFO;
1188 vattr.va_mode = (SCARG(uap, mode) & ALLPERMS) &~ p->p_fd->fd_cmask;
1189 VOP_LEASE(nd.ni_dvp, td, p->p_ucred, LEASE_WRITE);
1190 error = VOP_MKNOD(nd.ni_dvp, &nd.ni_vp, &nd.ni_cnd, &vattr);
1193 NDFREE(&nd, NDF_ONLY_PNBUF);
1199 * link_args(char *path, char *link)
1201 * Make a hard file link.
1205 link(struct link_args *uap)
1207 struct thread *td = curthread;
1208 struct proc *p = td->td_proc;
1210 struct nameidata nd;
1214 NDINIT(&nd, LOOKUP, FOLLOW|NOOBJ, UIO_USERSPACE, SCARG(uap, path), td);
1215 if ((error = namei(&nd)) != 0)
1217 NDFREE(&nd, NDF_ONLY_PNBUF);
1219 if (vp->v_type == VDIR)
1220 error = EPERM; /* POSIX */
1222 NDINIT(&nd, CREATE, LOCKPARENT|NOOBJ, UIO_USERSPACE, SCARG(uap, link), td);
1225 if (nd.ni_vp != NULL) {
1230 VOP_LEASE(nd.ni_dvp, td, p->p_ucred,
1232 VOP_LEASE(vp, td, p->p_ucred, LEASE_WRITE);
1233 error = VOP_LINK(nd.ni_dvp, vp, &nd.ni_cnd);
1235 NDFREE(&nd, NDF_ONLY_PNBUF);
1236 if (nd.ni_dvp == nd.ni_vp)
1243 ASSERT_VOP_UNLOCKED(nd.ni_dvp, "link");
1244 ASSERT_VOP_UNLOCKED(nd.ni_vp, "link");
1249 * symlink(char *path, char *link)
1251 * Make a symbolic link.
1255 symlink(struct symlink_args *uap)
1257 struct thread *td = curthread;
1258 struct proc *p = td->td_proc;
1262 struct nameidata nd;
1264 path = zalloc(namei_zone);
1265 if ((error = copyinstr(SCARG(uap, path), path, MAXPATHLEN, NULL)) != 0)
1268 NDINIT(&nd, CREATE, LOCKPARENT|NOOBJ, UIO_USERSPACE, SCARG(uap, link), td);
1269 if ((error = namei(&nd)) != 0)
1272 NDFREE(&nd, NDF_ONLY_PNBUF);
1273 if (nd.ni_dvp == nd.ni_vp)
1282 vattr.va_mode = ACCESSPERMS &~ p->p_fd->fd_cmask;
1283 VOP_LEASE(nd.ni_dvp, td, p->p_ucred, LEASE_WRITE);
1284 error = VOP_SYMLINK(nd.ni_dvp, &nd.ni_vp, &nd.ni_cnd, &vattr, path);
1285 NDFREE(&nd, NDF_ONLY_PNBUF);
1289 ASSERT_VOP_UNLOCKED(nd.ni_dvp, "symlink");
1290 ASSERT_VOP_UNLOCKED(nd.ni_vp, "symlink");
1292 zfree(namei_zone, path);
1297 * undelete_args(char *path)
1299 * Delete a whiteout from the filesystem.
1303 undelete(struct undelete_args *uap)
1305 struct thread *td = curthread;
1306 struct proc *p = td->td_proc;
1308 struct nameidata nd;
1311 NDINIT(&nd, DELETE, LOCKPARENT|DOWHITEOUT, UIO_USERSPACE,
1312 SCARG(uap, path), td);
1317 if (nd.ni_vp != NULLVP || !(nd.ni_cnd.cn_flags & ISWHITEOUT)) {
1318 NDFREE(&nd, NDF_ONLY_PNBUF);
1319 if (nd.ni_dvp == nd.ni_vp)
1328 VOP_LEASE(nd.ni_dvp, td, p->p_ucred, LEASE_WRITE);
1329 error = VOP_WHITEOUT(nd.ni_dvp, &nd.ni_cnd, DELETE);
1330 NDFREE(&nd, NDF_ONLY_PNBUF);
1332 ASSERT_VOP_UNLOCKED(nd.ni_dvp, "undelete");
1333 ASSERT_VOP_UNLOCKED(nd.ni_vp, "undelete");
1338 * unlink_args(char *path)
1340 * Delete a name from the filesystem.
1343 unlink(struct unlink_args *uap)
1345 struct thread *td = curthread;
1346 struct proc *p = td->td_proc;
1349 struct nameidata nd;
1352 NDINIT(&nd, DELETE, LOCKPARENT, UIO_USERSPACE, SCARG(uap, path), td);
1353 if ((error = namei(&nd)) != 0)
1356 VOP_LEASE(vp, td, p->p_ucred, LEASE_WRITE);
1357 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY, td);
1359 if (vp->v_type == VDIR)
1360 error = EPERM; /* POSIX */
1363 * The root of a mounted filesystem cannot be deleted.
1365 * XXX: can this only be a VDIR case?
1367 if (vp->v_flag & VROOT)
1372 VOP_LEASE(nd.ni_dvp, td, p->p_ucred, LEASE_WRITE);
1373 error = VOP_REMOVE(nd.ni_dvp, vp, &nd.ni_cnd);
1375 NDFREE(&nd, NDF_ONLY_PNBUF);
1376 if (nd.ni_dvp == vp)
1382 ASSERT_VOP_UNLOCKED(nd.ni_dvp, "unlink");
1383 ASSERT_VOP_UNLOCKED(nd.ni_vp, "unlink");
1388 * lseek_args(int fd, int pad, off_t offset, int whence)
1390 * Reposition read/write file offset.
1393 lseek(struct lseek_args *uap)
1395 struct thread *td = curthread;
1396 struct proc *p = td->td_proc;
1397 struct filedesc *fdp = p->p_fd;
1402 if ((u_int)SCARG(uap, fd) >= fdp->fd_nfiles ||
1403 (fp = fdp->fd_ofiles[SCARG(uap, fd)]) == NULL)
1405 if (fp->f_type != DTYPE_VNODE)
1407 switch (SCARG(uap, whence)) {
1409 fp->f_offset += SCARG(uap, offset);
1412 error=VOP_GETATTR((struct vnode *)fp->f_data, &vattr, td);
1415 fp->f_offset = SCARG(uap, offset) + vattr.va_size;
1418 fp->f_offset = SCARG(uap, offset);
1423 *(off_t *)(p->p_retval) = fp->f_offset;
1427 #if defined(COMPAT_43) || defined(COMPAT_SUNOS)
1429 * Reposition read/write file offset.
1431 * olseek_args(int fd, long offset, int whence)
1434 olseek(struct olseek_args *uap)
1436 struct lseek_args /* {
1438 syscallarg(int) pad;
1439 syscallarg(off_t) offset;
1440 syscallarg(int) whence;
1444 SCARG(&nuap, fd) = SCARG(uap, fd);
1445 SCARG(&nuap, offset) = SCARG(uap, offset);
1446 SCARG(&nuap, whence) = SCARG(uap, whence);
1447 error = lseek(&nuap);
1450 #endif /* COMPAT_43 */
1453 * access_args(char *path, int flags)
1455 * Check access permissions.
1458 access(struct access_args *uap)
1460 struct thread *td = curthread;
1461 struct proc *p = td->td_proc;
1462 struct ucred *cred, *tmpcred;
1465 struct nameidata nd;
1469 * Create and modify a temporary credential instead of one that
1470 * is potentially shared. This could also mess up socket
1471 * buffer accounting which can run in an interrupt context.
1473 tmpcred = crdup(cred);
1474 tmpcred->cr_uid = p->p_ucred->cr_ruid;
1475 tmpcred->cr_groups[0] = p->p_ucred->cr_rgid;
1476 p->p_ucred = tmpcred;
1477 NDINIT(&nd, LOOKUP, FOLLOW | LOCKLEAF | NOOBJ, UIO_USERSPACE,
1478 SCARG(uap, path), td);
1479 if ((error = namei(&nd)) != 0)
1483 /* Flags == 0 means only check for existence. */
1484 if (SCARG(uap, flags)) {
1486 if (SCARG(uap, flags) & R_OK)
1488 if (SCARG(uap, flags) & W_OK)
1490 if (SCARG(uap, flags) & X_OK)
1492 if ((flags & VWRITE) == 0 || (error = vn_writechk(vp)) == 0)
1493 error = VOP_ACCESS(vp, flags, tmpcred, td);
1495 NDFREE(&nd, NDF_ONLY_PNBUF);
1503 #if defined(COMPAT_43) || defined(COMPAT_SUNOS)
1505 * ostat_args(char *path, struct ostat *ub)
1507 * Get file status; this version follows links.
1511 ostat(struct ostat_args *uap)
1513 struct thread *td = curthread;
1517 struct nameidata nd;
1519 NDINIT(&nd, LOOKUP, FOLLOW | LOCKLEAF | NOOBJ, UIO_USERSPACE,
1520 SCARG(uap, path), td);
1521 if ((error = namei(&nd)) != 0)
1523 NDFREE(&nd, NDF_ONLY_PNBUF);
1524 error = vn_stat(nd.ni_vp, &sb, td);
1529 error = copyout((caddr_t)&osb, (caddr_t)SCARG(uap, ub), sizeof (osb));
1534 * olstat_args(char *path, struct ostat *ub)
1536 * Get file status; this version does not follow links.
1540 olstat(struct olstat_args *uap)
1542 struct thread *td = curthread;
1547 struct nameidata nd;
1549 NDINIT(&nd, LOOKUP, NOFOLLOW | LOCKLEAF | NOOBJ, UIO_USERSPACE,
1550 SCARG(uap, path), td);
1551 if ((error = namei(&nd)) != 0)
1554 error = vn_stat(vp, &sb, td);
1555 NDFREE(&nd, NDF_ONLY_PNBUF);
1560 error = copyout((caddr_t)&osb, (caddr_t)SCARG(uap, ub), sizeof (osb));
1565 * Convert from an old to a new stat structure.
1572 ost->st_dev = st->st_dev;
1573 ost->st_ino = st->st_ino;
1574 ost->st_mode = st->st_mode;
1575 ost->st_nlink = st->st_nlink;
1576 ost->st_uid = st->st_uid;
1577 ost->st_gid = st->st_gid;
1578 ost->st_rdev = st->st_rdev;
1579 if (st->st_size < (quad_t)1 << 32)
1580 ost->st_size = st->st_size;
1583 ost->st_atime = st->st_atime;
1584 ost->st_mtime = st->st_mtime;
1585 ost->st_ctime = st->st_ctime;
1586 ost->st_blksize = st->st_blksize;
1587 ost->st_blocks = st->st_blocks;
1588 ost->st_flags = st->st_flags;
1589 ost->st_gen = st->st_gen;
1591 #endif /* COMPAT_43 || COMPAT_SUNOS */
1594 * stat_args(char *path, struct stat *ub)
1596 * Get file status; this version follows links.
1600 stat(struct stat_args *uap)
1602 struct thread *td = curthread;
1605 struct nameidata nd;
1607 NDINIT(&nd, LOOKUP, FOLLOW | LOCKLEAF | NOOBJ, UIO_USERSPACE,
1608 SCARG(uap, path), td);
1609 if ((error = namei(&nd)) != 0)
1611 error = vn_stat(nd.ni_vp, &sb, td);
1612 NDFREE(&nd, NDF_ONLY_PNBUF);
1616 error = copyout((caddr_t)&sb, (caddr_t)SCARG(uap, ub), sizeof (sb));
1621 * lstat_args(char *path, struct stat *ub)
1623 * Get file status; this version does not follow links.
1627 lstat(struct lstat_args *uap)
1629 struct thread *td = curthread;
1633 struct nameidata nd;
1635 NDINIT(&nd, LOOKUP, NOFOLLOW | LOCKLEAF | NOOBJ, UIO_USERSPACE,
1636 SCARG(uap, path), td);
1637 if ((error = namei(&nd)) != 0)
1640 error = vn_stat(vp, &sb, td);
1641 NDFREE(&nd, NDF_ONLY_PNBUF);
1645 error = copyout((caddr_t)&sb, (caddr_t)SCARG(uap, ub), sizeof (sb));
1654 nsb->st_dev = sb->st_dev;
1655 nsb->st_ino = sb->st_ino;
1656 nsb->st_mode = sb->st_mode;
1657 nsb->st_nlink = sb->st_nlink;
1658 nsb->st_uid = sb->st_uid;
1659 nsb->st_gid = sb->st_gid;
1660 nsb->st_rdev = sb->st_rdev;
1661 nsb->st_atimespec = sb->st_atimespec;
1662 nsb->st_mtimespec = sb->st_mtimespec;
1663 nsb->st_ctimespec = sb->st_ctimespec;
1664 nsb->st_size = sb->st_size;
1665 nsb->st_blocks = sb->st_blocks;
1666 nsb->st_blksize = sb->st_blksize;
1667 nsb->st_flags = sb->st_flags;
1668 nsb->st_gen = sb->st_gen;
1669 nsb->st_qspare[0] = sb->st_qspare[0];
1670 nsb->st_qspare[1] = sb->st_qspare[1];
1674 * nstat_args(char *path, struct nstat *ub)
1678 nstat(struct nstat_args *uap)
1680 struct thread *td = curthread;
1684 struct nameidata nd;
1686 NDINIT(&nd, LOOKUP, FOLLOW | LOCKLEAF | NOOBJ, UIO_USERSPACE,
1687 SCARG(uap, path), td);
1688 if ((error = namei(&nd)) != 0)
1690 NDFREE(&nd, NDF_ONLY_PNBUF);
1691 error = vn_stat(nd.ni_vp, &sb, td);
1695 cvtnstat(&sb, &nsb);
1696 error = copyout((caddr_t)&nsb, (caddr_t)SCARG(uap, ub), sizeof (nsb));
1701 * lstat_args(char *path, struct stat *ub)
1703 * Get file status; this version does not follow links.
1707 nlstat(struct nlstat_args *uap)
1709 struct thread *td = curthread;
1714 struct nameidata nd;
1716 NDINIT(&nd, LOOKUP, NOFOLLOW | LOCKLEAF | NOOBJ, UIO_USERSPACE,
1717 SCARG(uap, path), td);
1718 if ((error = namei(&nd)) != 0)
1721 NDFREE(&nd, NDF_ONLY_PNBUF);
1722 error = vn_stat(vp, &sb, td);
1726 cvtnstat(&sb, &nsb);
1727 error = copyout((caddr_t)&nsb, (caddr_t)SCARG(uap, ub), sizeof (nsb));
1732 * pathconf_Args(char *path, int name)
1734 * Get configurable pathname variables.
1738 pathconf(struct pathconf_args *uap)
1740 struct thread *td = curthread;
1741 struct proc *p = td->td_proc;
1743 struct nameidata nd;
1745 NDINIT(&nd, LOOKUP, FOLLOW | LOCKLEAF | NOOBJ, UIO_USERSPACE,
1746 SCARG(uap, path), td);
1747 if ((error = namei(&nd)) != 0)
1749 NDFREE(&nd, NDF_ONLY_PNBUF);
1750 error = VOP_PATHCONF(nd.ni_vp, SCARG(uap, name), p->p_retval);
1756 * readlink_args(char *path, char *buf, int count)
1758 * Return target name of a symbolic link.
1762 readlink(struct readlink_args *uap)
1764 struct thread *td = curthread;
1765 struct proc *p = td->td_proc;
1770 struct nameidata nd;
1772 NDINIT(&nd, LOOKUP, NOFOLLOW | LOCKLEAF | NOOBJ, UIO_USERSPACE,
1773 SCARG(uap, path), td);
1774 if ((error = namei(&nd)) != 0)
1776 NDFREE(&nd, NDF_ONLY_PNBUF);
1778 if (vp->v_type != VLNK)
1781 aiov.iov_base = SCARG(uap, buf);
1782 aiov.iov_len = SCARG(uap, count);
1783 auio.uio_iov = &aiov;
1784 auio.uio_iovcnt = 1;
1785 auio.uio_offset = 0;
1786 auio.uio_rw = UIO_READ;
1787 auio.uio_segflg = UIO_USERSPACE;
1789 auio.uio_resid = SCARG(uap, count);
1790 error = VOP_READLINK(vp, &auio, p->p_ucred);
1793 p->p_retval[0] = SCARG(uap, count) - auio.uio_resid;
1798 setfflags(struct vnode *vp, int flags)
1800 struct thread *td = curthread;
1801 struct proc *p = td->td_proc;
1806 * Prevent non-root users from setting flags on devices. When
1807 * a device is reused, users can retain ownership of the device
1808 * if they are allowed to set flags and programs assume that
1809 * chown can't fail when done as root.
1811 if ((vp->v_type == VCHR || vp->v_type == VBLK) &&
1812 ((error = suser_cred(p->p_ucred, PRISON_ROOT)) != 0))
1815 VOP_LEASE(vp, td, p->p_ucred, LEASE_WRITE);
1816 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY, td);
1818 vattr.va_flags = flags;
1819 error = VOP_SETATTR(vp, &vattr, p->p_ucred, td);
1820 VOP_UNLOCK(vp, 0, td);
1825 * chflags(char *path, int flags)
1827 * Change flags of a file given a path name.
1831 chflags(struct chflags_args *uap)
1833 struct thread *td = curthread;
1835 struct nameidata nd;
1837 NDINIT(&nd, LOOKUP, FOLLOW, UIO_USERSPACE, SCARG(uap, path), td);
1838 if ((error = namei(&nd)) != 0)
1840 NDFREE(&nd, NDF_ONLY_PNBUF);
1841 error = setfflags(nd.ni_vp, SCARG(uap, flags));
1847 * fchflags_args(int fd, int flags)
1849 * Change flags of a file given a file descriptor.
1853 fchflags(struct fchflags_args *uap)
1855 struct thread *td = curthread;
1856 struct proc *p = td->td_proc;
1860 if ((error = getvnode(p->p_fd, SCARG(uap, fd), &fp)) != 0)
1862 return setfflags((struct vnode *) fp->f_data, SCARG(uap, flags));
1866 setfmode(struct vnode *vp, int mode)
1868 struct thread *td = curthread;
1869 struct proc *p = td->td_proc;
1873 VOP_LEASE(vp, td, p->p_ucred, LEASE_WRITE);
1874 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY, td);
1876 vattr.va_mode = mode & ALLPERMS;
1877 error = VOP_SETATTR(vp, &vattr, p->p_ucred, td);
1878 VOP_UNLOCK(vp, 0, td);
1883 * chmod_args(char *path, int mode)
1885 * Change mode of a file given path name.
1889 chmod(struct chmod_args *uap)
1891 struct thread *td = curthread;
1893 struct nameidata nd;
1895 NDINIT(&nd, LOOKUP, FOLLOW, UIO_USERSPACE, SCARG(uap, path), td);
1896 if ((error = namei(&nd)) != 0)
1898 NDFREE(&nd, NDF_ONLY_PNBUF);
1899 error = setfmode(nd.ni_vp, SCARG(uap, mode));
1905 * lchmod_args(char *path, int mode)
1907 * Change mode of a file given path name (don't follow links.)
1911 lchmod(struct lchmod_args *uap)
1913 struct thread *td = curthread;
1915 struct nameidata nd;
1917 NDINIT(&nd, LOOKUP, NOFOLLOW, 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 * fchmod_args(int fd, int mode)
1929 * Change mode of a file given a file descriptor.
1933 fchmod(struct fchmod_args *uap)
1935 struct thread *td = curthread;
1936 struct proc *p = td->td_proc;
1940 if ((error = getvnode(p->p_fd, SCARG(uap, fd), &fp)) != 0)
1942 return setfmode((struct vnode *)fp->f_data, SCARG(uap, mode));
1946 setfown(struct vnode *vp, uid_t uid, gid_t gid)
1948 struct thread *td = curthread;
1949 struct proc *p = td->td_proc;
1953 VOP_LEASE(vp, td, p->p_ucred, LEASE_WRITE);
1954 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY, td);
1958 error = VOP_SETATTR(vp, &vattr, p->p_ucred, td);
1959 VOP_UNLOCK(vp, 0, td);
1964 * chown(char *path, int uid, int gid)
1966 * Set ownership given a path name.
1970 chown(struct chown_args *uap)
1972 struct thread *td = curthread;
1974 struct nameidata nd;
1976 NDINIT(&nd, LOOKUP, FOLLOW, UIO_USERSPACE, SCARG(uap, path), td);
1977 if ((error = namei(&nd)) != 0)
1979 NDFREE(&nd, NDF_ONLY_PNBUF);
1980 error = setfown(nd.ni_vp, SCARG(uap, uid), SCARG(uap, gid));
1986 * lchown_args(char *path, int uid, int gid)
1988 * Set ownership given a path name, do not cross symlinks.
1992 lchown(struct lchown_args *uap)
1994 struct thread *td = curthread;
1996 struct nameidata nd;
1998 NDINIT(&nd, LOOKUP, NOFOLLOW, 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 * fchown_args(int fd, int uid, int gid)
2010 * Set ownership given a file descriptor.
2014 fchown(struct fchown_args *uap)
2016 struct thread *td = curthread;
2017 struct proc *p = td->td_proc;
2021 if ((error = getvnode(p->p_fd, SCARG(uap, fd), &fp)) != 0)
2023 return setfown((struct vnode *)fp->f_data,
2024 SCARG(uap, uid), SCARG(uap, gid));
2028 getutimes(const struct timeval *usrtvp, struct timespec *tsp)
2030 struct timeval tv[2];
2033 if (usrtvp == NULL) {
2035 TIMEVAL_TO_TIMESPEC(&tv[0], &tsp[0]);
2038 if ((error = copyin(usrtvp, tv, sizeof (tv))) != 0)
2040 TIMEVAL_TO_TIMESPEC(&tv[0], &tsp[0]);
2041 TIMEVAL_TO_TIMESPEC(&tv[1], &tsp[1]);
2047 setutimes(struct vnode *vp, const struct timespec *ts, int nullflag)
2049 struct thread *td = curthread;
2050 struct proc *p = td->td_proc;
2054 VOP_LEASE(vp, td, p->p_ucred, LEASE_WRITE);
2055 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY, td);
2057 vattr.va_atime = ts[0];
2058 vattr.va_mtime = ts[1];
2060 vattr.va_vaflags |= VA_UTIMES_NULL;
2061 error = VOP_SETATTR(vp, &vattr, p->p_ucred, td);
2062 VOP_UNLOCK(vp, 0, td);
2067 * utimes_args(char *path, struct timeval *tptr)
2069 * Set the access and modification times of a file.
2073 utimes(struct utimes_args *uap)
2075 struct thread *td = curthread;
2076 struct timespec ts[2];
2077 struct timeval *usrtvp;
2079 struct nameidata nd;
2081 usrtvp = SCARG(uap, tptr);
2082 if ((error = getutimes(usrtvp, ts)) != 0)
2084 NDINIT(&nd, LOOKUP, FOLLOW, UIO_USERSPACE, SCARG(uap, path), td);
2085 if ((error = namei(&nd)) != 0)
2087 NDFREE(&nd, NDF_ONLY_PNBUF);
2088 error = setutimes(nd.ni_vp, ts, usrtvp == NULL);
2094 * lutimes_args(char *path, struct timeval *tptr)
2096 * Set the access and modification times of a file.
2100 lutimes(struct lutimes_args *uap)
2102 struct thread *td = curthread;
2103 struct timespec ts[2];
2104 struct timeval *usrtvp;
2106 struct nameidata nd;
2108 usrtvp = SCARG(uap, tptr);
2109 if ((error = getutimes(usrtvp, ts)) != 0)
2111 NDINIT(&nd, LOOKUP, NOFOLLOW, UIO_USERSPACE, SCARG(uap, path), td);
2112 if ((error = namei(&nd)) != 0)
2114 NDFREE(&nd, NDF_ONLY_PNBUF);
2115 error = setutimes(nd.ni_vp, ts, usrtvp == NULL);
2121 * futimes_args(int fd, struct timeval *tptr)
2123 * Set the access and modification times of a file.
2127 futimes(struct futimes_args *uap)
2129 struct thread *td = curthread;
2130 struct proc *p = td->td_proc;
2131 struct timespec ts[2];
2133 struct timeval *usrtvp;
2136 usrtvp = SCARG(uap, tptr);
2137 if ((error = getutimes(usrtvp, ts)) != 0)
2139 if ((error = getvnode(p->p_fd, SCARG(uap, fd), &fp)) != 0)
2141 return setutimes((struct vnode *)fp->f_data, ts, usrtvp == NULL);
2145 * truncate(char *path, int pad, off_t length)
2147 * Truncate a file given its path name.
2151 truncate(struct truncate_args *uap)
2153 struct thread *td = curthread;
2154 struct proc *p = td->td_proc;
2158 struct nameidata nd;
2160 if (uap->length < 0)
2162 NDINIT(&nd, LOOKUP, FOLLOW, UIO_USERSPACE, SCARG(uap, path), td);
2163 if ((error = namei(&nd)) != 0)
2166 NDFREE(&nd, NDF_ONLY_PNBUF);
2167 VOP_LEASE(vp, td, p->p_ucred, LEASE_WRITE);
2168 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY, td);
2169 if (vp->v_type == VDIR)
2171 else if ((error = vn_writechk(vp)) == 0 &&
2172 (error = VOP_ACCESS(vp, VWRITE, p->p_ucred, td)) == 0) {
2174 vattr.va_size = SCARG(uap, length);
2175 error = VOP_SETATTR(vp, &vattr, p->p_ucred, td);
2182 * ftruncate_args(int fd, int pad, off_t length)
2184 * Truncate a file given a file descriptor.
2188 ftruncate(struct ftruncate_args *uap)
2190 struct thread *td = curthread;
2191 struct proc *p = td->td_proc;
2197 if (uap->length < 0)
2199 if ((error = getvnode(p->p_fd, SCARG(uap, fd), &fp)) != 0)
2201 if ((fp->f_flag & FWRITE) == 0)
2203 vp = (struct vnode *)fp->f_data;
2204 VOP_LEASE(vp, td, p->p_ucred, LEASE_WRITE);
2205 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY, td);
2206 if (vp->v_type == VDIR)
2208 else if ((error = vn_writechk(vp)) == 0) {
2210 vattr.va_size = SCARG(uap, length);
2211 error = VOP_SETATTR(vp, &vattr, fp->f_cred, td);
2213 VOP_UNLOCK(vp, 0, td);
2217 #if defined(COMPAT_43) || defined(COMPAT_SUNOS)
2219 * otruncate_args(char *path, long length)
2221 * Truncate a file given its path name.
2225 otruncate(struct otruncate_args *uap)
2227 struct truncate_args /* {
2228 syscallarg(char *) path;
2229 syscallarg(int) pad;
2230 syscallarg(off_t) length;
2233 SCARG(&nuap, path) = SCARG(uap, path);
2234 SCARG(&nuap, length) = SCARG(uap, length);
2235 return (truncate(&nuap));
2239 * oftruncate_args(int fd, long length)
2241 * Truncate a file given a file descriptor.
2245 oftruncate(struct oftruncate_args *uap)
2247 struct ftruncate_args /* {
2249 syscallarg(int) pad;
2250 syscallarg(off_t) length;
2253 SCARG(&nuap, fd) = SCARG(uap, fd);
2254 SCARG(&nuap, length) = SCARG(uap, length);
2255 return (ftruncate(&nuap));
2257 #endif /* COMPAT_43 || COMPAT_SUNOS */
2262 * Sync an open file.
2266 fsync(struct fsync_args *uap)
2268 struct thread *td = curthread;
2269 struct proc *p = td->td_proc;
2275 if ((error = getvnode(p->p_fd, SCARG(uap, fd), &fp)) != 0)
2277 vp = (struct vnode *)fp->f_data;
2278 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY, td);
2279 if (VOP_GETVOBJECT(vp, &obj) == 0)
2280 vm_object_page_clean(obj, 0, 0, 0);
2281 if ((error = VOP_FSYNC(vp, MNT_WAIT, td)) == 0 &&
2282 vp->v_mount && (vp->v_mount->mnt_flag & MNT_SOFTDEP) &&
2284 error = (*bioops.io_fsync)(vp);
2285 VOP_UNLOCK(vp, 0, td);
2290 * rename_args(char *from, char *to)
2292 * Rename files. Source and destination must either both be directories,
2293 * or both not be directories. If target is a directory, it must be empty.
2297 rename(struct rename_args *uap)
2299 struct thread *td = curthread;
2300 struct proc *p = td->td_proc;
2301 struct vnode *tvp, *fvp, *tdvp;
2302 struct nameidata fromnd, tond;
2306 NDINIT(&fromnd, DELETE, WANTPARENT | SAVESTART, UIO_USERSPACE,
2307 SCARG(uap, from), td);
2308 if ((error = namei(&fromnd)) != 0)
2311 NDINIT(&tond, RENAME, LOCKPARENT | LOCKLEAF | NOCACHE | SAVESTART | NOOBJ,
2312 UIO_USERSPACE, SCARG(uap, to), td);
2313 if (fromnd.ni_vp->v_type == VDIR)
2314 tond.ni_cnd.cn_flags |= WILLBEDIR;
2315 if ((error = namei(&tond)) != 0) {
2316 /* Translate error code for rename("dir1", "dir2/."). */
2317 if (error == EISDIR && fvp->v_type == VDIR)
2319 NDFREE(&fromnd, NDF_ONLY_PNBUF);
2320 vrele(fromnd.ni_dvp);
2327 if (fvp->v_type == VDIR && tvp->v_type != VDIR) {
2330 } else if (fvp->v_type != VDIR && tvp->v_type == VDIR) {
2338 * If the source is the same as the destination (that is, if they
2339 * are links to the same vnode), then there is nothing to do.
2345 VOP_LEASE(tdvp, td, p->p_ucred, LEASE_WRITE);
2346 if (fromnd.ni_dvp != tdvp) {
2347 VOP_LEASE(fromnd.ni_dvp, td, p->p_ucred, LEASE_WRITE);
2350 VOP_LEASE(tvp, td, p->p_ucred, LEASE_WRITE);
2352 error = VOP_RENAME(fromnd.ni_dvp, fromnd.ni_vp, &fromnd.ni_cnd,
2353 tond.ni_dvp, tond.ni_vp, &tond.ni_cnd);
2354 NDFREE(&fromnd, NDF_ONLY_PNBUF);
2355 NDFREE(&tond, NDF_ONLY_PNBUF);
2357 NDFREE(&fromnd, NDF_ONLY_PNBUF);
2358 NDFREE(&tond, NDF_ONLY_PNBUF);
2365 vrele(fromnd.ni_dvp);
2368 vrele(tond.ni_startdir);
2369 ASSERT_VOP_UNLOCKED(fromnd.ni_dvp, "rename");
2370 ASSERT_VOP_UNLOCKED(fromnd.ni_vp, "rename");
2371 ASSERT_VOP_UNLOCKED(tond.ni_dvp, "rename");
2372 ASSERT_VOP_UNLOCKED(tond.ni_vp, "rename");
2374 if (fromnd.ni_startdir)
2375 vrele(fromnd.ni_startdir);
2382 * mkdir_args(char *path, int mode)
2384 * Make a directory file.
2388 mkdir(struct mkdir_args *uap)
2390 struct thread *td = curthread;
2391 struct proc *p = td->td_proc;
2395 struct nameidata nd;
2398 NDINIT(&nd, CREATE, LOCKPARENT, UIO_USERSPACE, SCARG(uap, path), td);
2399 nd.ni_cnd.cn_flags |= WILLBEDIR;
2400 if ((error = namei(&nd)) != 0)
2404 NDFREE(&nd, NDF_ONLY_PNBUF);
2405 if (nd.ni_dvp == vp)
2413 vattr.va_type = VDIR;
2414 vattr.va_mode = (SCARG(uap, mode) & ACCESSPERMS) &~ p->p_fd->fd_cmask;
2415 VOP_LEASE(nd.ni_dvp, td, p->p_ucred, LEASE_WRITE);
2416 error = VOP_MKDIR(nd.ni_dvp, &nd.ni_vp, &nd.ni_cnd, &vattr);
2417 NDFREE(&nd, NDF_ONLY_PNBUF);
2421 ASSERT_VOP_UNLOCKED(nd.ni_dvp, "mkdir");
2422 ASSERT_VOP_UNLOCKED(nd.ni_vp, "mkdir");
2427 * rmdir_args(char *path)
2429 * Remove a directory file.
2433 rmdir(struct rmdir_args *uap)
2435 struct thread *td = curthread;
2436 struct proc *p = td->td_proc;
2439 struct nameidata nd;
2442 NDINIT(&nd, DELETE, LOCKPARENT | LOCKLEAF, UIO_USERSPACE,
2443 SCARG(uap, path), td);
2444 if ((error = namei(&nd)) != 0)
2447 if (vp->v_type != VDIR) {
2452 * No rmdir "." please.
2454 if (nd.ni_dvp == vp) {
2459 * The root of a mounted filesystem cannot be deleted.
2461 if (vp->v_flag & VROOT)
2464 VOP_LEASE(nd.ni_dvp, td, p->p_ucred, LEASE_WRITE);
2465 VOP_LEASE(vp, td, p->p_ucred, LEASE_WRITE);
2466 error = VOP_RMDIR(nd.ni_dvp, nd.ni_vp, &nd.ni_cnd);
2469 NDFREE(&nd, NDF_ONLY_PNBUF);
2470 if (nd.ni_dvp == vp)
2476 ASSERT_VOP_UNLOCKED(nd.ni_dvp, "rmdir");
2477 ASSERT_VOP_UNLOCKED(nd.ni_vp, "rmdir");
2483 * ogetdirentries_args(int fd, char *buf, u_int count, long *basep)
2485 * Read a block of directory entries in a file system independent format.
2488 ogetdirentries(struct ogetdirentries_args *uap)
2490 struct thread *td = curthread;
2491 struct proc *p = td->td_proc;
2494 struct uio auio, kuio;
2495 struct iovec aiov, kiov;
2496 struct dirent *dp, *edp;
2498 int error, eofflag, readcnt;
2501 /* XXX arbitrary sanity limit on `count'. */
2502 if (SCARG(uap, count) > 64 * 1024)
2504 if ((error = getvnode(p->p_fd, SCARG(uap, fd), &fp)) != 0)
2506 if ((fp->f_flag & FREAD) == 0)
2508 vp = (struct vnode *)fp->f_data;
2510 if (vp->v_type != VDIR)
2512 aiov.iov_base = SCARG(uap, buf);
2513 aiov.iov_len = SCARG(uap, count);
2514 auio.uio_iov = &aiov;
2515 auio.uio_iovcnt = 1;
2516 auio.uio_rw = UIO_READ;
2517 auio.uio_segflg = UIO_USERSPACE;
2519 auio.uio_resid = SCARG(uap, count);
2520 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY, td);
2521 loff = auio.uio_offset = fp->f_offset;
2522 # if (BYTE_ORDER != LITTLE_ENDIAN)
2523 if (vp->v_mount->mnt_maxsymlinklen <= 0) {
2524 error = VOP_READDIR(vp, &auio, fp->f_cred, &eofflag,
2526 fp->f_offset = auio.uio_offset;
2531 kuio.uio_iov = &kiov;
2532 kuio.uio_segflg = UIO_SYSSPACE;
2533 kiov.iov_len = SCARG(uap, count);
2534 MALLOC(dirbuf, caddr_t, SCARG(uap, count), M_TEMP, M_WAITOK);
2535 kiov.iov_base = dirbuf;
2536 error = VOP_READDIR(vp, &kuio, fp->f_cred, &eofflag,
2538 fp->f_offset = kuio.uio_offset;
2540 readcnt = SCARG(uap, count) - kuio.uio_resid;
2541 edp = (struct dirent *)&dirbuf[readcnt];
2542 for (dp = (struct dirent *)dirbuf; dp < edp; ) {
2543 # if (BYTE_ORDER == LITTLE_ENDIAN)
2545 * The expected low byte of
2546 * dp->d_namlen is our dp->d_type.
2547 * The high MBZ byte of dp->d_namlen
2548 * is our dp->d_namlen.
2550 dp->d_type = dp->d_namlen;
2554 * The dp->d_type is the high byte
2555 * of the expected dp->d_namlen,
2556 * so must be zero'ed.
2560 if (dp->d_reclen > 0) {
2561 dp = (struct dirent *)
2562 ((char *)dp + dp->d_reclen);
2569 error = uiomove(dirbuf, readcnt, &auio);
2571 FREE(dirbuf, M_TEMP);
2573 VOP_UNLOCK(vp, 0, td);
2576 if (SCARG(uap, count) == auio.uio_resid) {
2577 if (union_dircheckp) {
2578 error = union_dircheckp(td, &vp, fp);
2584 if ((vp->v_flag & VROOT) &&
2585 (vp->v_mount->mnt_flag & MNT_UNION)) {
2586 struct vnode *tvp = vp;
2587 vp = vp->v_mount->mnt_vnodecovered;
2589 fp->f_data = (caddr_t) vp;
2595 error = copyout((caddr_t)&loff, (caddr_t)SCARG(uap, basep),
2597 p->p_retval[0] = SCARG(uap, count) - auio.uio_resid;
2600 #endif /* COMPAT_43 */
2603 * getdirentries_args(int fd, char *buf, u_int conut, long *basep)
2605 * Read a block of directory entries in a file system independent format.
2608 getdirentries(struct getdirentries_args *uap)
2610 struct thread *td = curthread;
2611 struct proc *p = td->td_proc;
2619 if ((error = getvnode(p->p_fd, SCARG(uap, fd), &fp)) != 0)
2621 if ((fp->f_flag & FREAD) == 0)
2623 vp = (struct vnode *)fp->f_data;
2625 if (vp->v_type != VDIR)
2627 aiov.iov_base = SCARG(uap, buf);
2628 aiov.iov_len = SCARG(uap, count);
2629 auio.uio_iov = &aiov;
2630 auio.uio_iovcnt = 1;
2631 auio.uio_rw = UIO_READ;
2632 auio.uio_segflg = UIO_USERSPACE;
2634 auio.uio_resid = SCARG(uap, count);
2635 /* vn_lock(vp, LK_SHARED | LK_RETRY, td); */
2636 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY, td);
2637 loff = auio.uio_offset = fp->f_offset;
2638 error = VOP_READDIR(vp, &auio, fp->f_cred, &eofflag, NULL, NULL);
2639 fp->f_offset = auio.uio_offset;
2640 VOP_UNLOCK(vp, 0, td);
2643 if (SCARG(uap, count) == auio.uio_resid) {
2644 if (union_dircheckp) {
2645 error = union_dircheckp(td, &vp, fp);
2651 if ((vp->v_flag & VROOT) &&
2652 (vp->v_mount->mnt_flag & MNT_UNION)) {
2653 struct vnode *tvp = vp;
2654 vp = vp->v_mount->mnt_vnodecovered;
2656 fp->f_data = (caddr_t) vp;
2662 if (SCARG(uap, basep) != NULL) {
2663 error = copyout((caddr_t)&loff, (caddr_t)SCARG(uap, basep),
2666 p->p_retval[0] = SCARG(uap, count) - auio.uio_resid;
2671 * getdents_args(int fd, char *buf, size_t count)
2674 getdents(struct getdents_args *uap)
2676 struct getdirentries_args ap;
2680 ap.count = uap->count;
2682 return getdirentries(&ap);
2686 * umask(int newmask)
2688 * Set the mode mask for creation of filesystem nodes.
2693 umask(struct umask_args *uap)
2695 struct thread *td = curthread;
2696 struct proc *p = td->td_proc;
2697 struct filedesc *fdp;
2700 p->p_retval[0] = fdp->fd_cmask;
2701 fdp->fd_cmask = SCARG(uap, newmask) & ALLPERMS;
2706 * revoke(char *path)
2708 * Void all references to file by ripping underlying filesystem
2713 revoke(struct revoke_args *uap)
2715 struct thread *td = curthread;
2716 struct proc *p = td->td_proc;
2720 struct nameidata nd;
2722 NDINIT(&nd, LOOKUP, FOLLOW, UIO_USERSPACE, SCARG(uap, path), td);
2723 if ((error = namei(&nd)) != 0)
2726 NDFREE(&nd, NDF_ONLY_PNBUF);
2727 if (vp->v_type != VCHR && vp->v_type != VBLK) {
2731 if ((error = VOP_GETATTR(vp, &vattr, td)) != 0)
2733 if (p->p_ucred->cr_uid != vattr.va_uid &&
2734 (error = suser_cred(p->p_ucred, PRISON_ROOT)))
2737 VOP_REVOKE(vp, REVOKEALL);
2744 * Convert a user file descriptor to a kernel file entry.
2747 getvnode(struct filedesc *fdp, int fd, struct file **fpp)
2751 if ((u_int)fd >= fdp->fd_nfiles ||
2752 (fp = fdp->fd_ofiles[fd]) == NULL)
2754 if (fp->f_type != DTYPE_VNODE && fp->f_type != DTYPE_FIFO)
2760 * getfh_args(char *fname, fhandle_t *fhp)
2762 * Get (NFS) file handle
2765 getfh(struct getfh_args *uap)
2767 struct thread *td = curthread;
2768 struct nameidata nd;
2774 * Must be super user
2779 NDINIT(&nd, LOOKUP, FOLLOW | LOCKLEAF, UIO_USERSPACE, uap->fname, td);
2783 NDFREE(&nd, NDF_ONLY_PNBUF);
2785 bzero(&fh, sizeof(fh));
2786 fh.fh_fsid = vp->v_mount->mnt_stat.f_fsid;
2787 error = VFS_VPTOFH(vp, &fh.fh_fid);
2791 error = copyout(&fh, uap->fhp, sizeof (fh));
2796 * fhopen_args(const struct fhandle *u_fhp, int flags)
2798 * syscall for the rpc.lockd to use to translate a NFS file handle into
2799 * an open descriptor.
2801 * warning: do not remove the suser() call or this becomes one giant
2805 fhopen(struct fhopen_args *uap)
2807 struct thread *td = curthread;
2808 struct proc *p = td->td_proc;
2813 struct vattr *vap = &vat;
2816 struct filedesc *fdp = p->p_fd;
2817 int fmode, mode, error, type;
2822 * Must be super user
2828 fmode = FFLAGS(SCARG(uap, flags));
2829 /* why not allow a non-read/write open for our lockd? */
2830 if (((fmode & (FREAD | FWRITE)) == 0) || (fmode & O_CREAT))
2832 error = copyin(SCARG(uap,u_fhp), &fhp, sizeof(fhp));
2835 /* find the mount point */
2836 mp = vfs_getvfs(&fhp.fh_fsid);
2839 /* now give me my vnode, it gets returned to me locked */
2840 error = VFS_FHTOVP(mp, &fhp.fh_fid, &vp);
2844 * from now on we have to make sure not
2845 * to forget about the vnode
2846 * any error that causes an abort must vput(vp)
2847 * just set error = err and 'goto bad;'.
2853 if (vp->v_type == VLNK) {
2857 if (vp->v_type == VSOCK) {
2862 if (fmode & (FWRITE | O_TRUNC)) {
2863 if (vp->v_type == VDIR) {
2867 error = vn_writechk(vp);
2875 error = VOP_ACCESS(vp, mode, p->p_ucred, td);
2879 if (fmode & O_TRUNC) {
2880 VOP_UNLOCK(vp, 0, td); /* XXX */
2881 VOP_LEASE(vp, td, p->p_ucred, LEASE_WRITE);
2882 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY, td); /* XXX */
2885 error = VOP_SETATTR(vp, vap, p->p_ucred, td);
2889 error = VOP_OPEN(vp, fmode, p->p_ucred, td);
2893 * Make sure that a VM object is created for VMIO support.
2895 if (vn_canvmio(vp) == TRUE) {
2896 if ((error = vfs_object_create(vp, td)) != 0)
2903 * end of vn_open code
2906 if ((error = falloc(p, &nfp, &indx)) != 0) {
2914 * hold an extra reference to avoid having fp ripped out
2915 * from under us while we block in the lock op.
2918 nfp->f_data = (caddr_t)vp;
2919 nfp->f_flag = fmode & FMASK;
2920 nfp->f_ops = &vnops;
2921 nfp->f_type = DTYPE_VNODE;
2922 if (fmode & (O_EXLOCK | O_SHLOCK)) {
2923 lf.l_whence = SEEK_SET;
2926 if (fmode & O_EXLOCK)
2927 lf.l_type = F_WRLCK;
2929 lf.l_type = F_RDLCK;
2931 if ((fmode & FNONBLOCK) == 0)
2933 VOP_UNLOCK(vp, 0, td);
2934 if ((error = VOP_ADVLOCK(vp, (caddr_t)fp, F_SETLK, &lf, type)) != 0) {
2936 * lock request failed. Normally close the descriptor
2937 * but handle the case where someone might have dup()d
2938 * or close()d it when we weren't looking.
2940 if (fdp->fd_ofiles[indx] == fp) {
2941 fdp->fd_ofiles[indx] = NULL;
2946 * release our private reference.
2951 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY, td);
2952 fp->f_flag |= FHASLOCK;
2954 if ((vp->v_type == VREG) && (VOP_GETVOBJECT(vp, NULL) != 0))
2955 vfs_object_create(vp, td);
2957 VOP_UNLOCK(vp, 0, td);
2959 p->p_retval[0] = indx;
2968 * fhstat_args(struct fhandle *u_fhp, struct stat *sb)
2971 fhstat(struct fhstat_args *uap)
2973 struct thread *td = curthread;
2981 * Must be super user
2987 error = copyin(SCARG(uap, u_fhp), &fh, sizeof(fhandle_t));
2991 if ((mp = vfs_getvfs(&fh.fh_fsid)) == NULL)
2993 if ((error = VFS_FHTOVP(mp, &fh.fh_fid, &vp)))
2995 error = vn_stat(vp, &sb, td);
2999 error = copyout(&sb, SCARG(uap, sb), sizeof(sb));
3004 * fhstatfs_args(struct fhandle *u_fhp, struct statfs *buf)
3007 fhstatfs(struct fhstatfs_args *uap)
3009 struct thread *td = curthread;
3018 * Must be super user
3020 if ((error = suser(td)))
3023 if ((error = copyin(SCARG(uap, u_fhp), &fh, sizeof(fhandle_t))) != 0)
3026 if ((mp = vfs_getvfs(&fh.fh_fsid)) == NULL)
3028 if ((error = VFS_FHTOVP(mp, &fh.fh_fid, &vp)))
3033 if ((error = VFS_STATFS(mp, sp, td)) != 0)
3035 sp->f_flags = mp->mnt_flag & MNT_VISFLAGMASK;
3037 bcopy((caddr_t)sp, (caddr_t)&sb, sizeof(sb));
3038 sb.f_fsid.val[0] = sb.f_fsid.val[1] = 0;
3041 return (copyout(sp, SCARG(uap, buf), sizeof(*sp)));
3045 * Syscall to push extended attribute configuration information into the
3046 * VFS. Accepts a path, which it converts to a mountpoint, as well as
3047 * a command (int cmd), and attribute name and misc data. For now, the
3048 * attribute name is left in userspace for consumption by the VFS_op.
3049 * It will probably be changed to be copied into sysspace by the
3050 * syscall in the future, once issues with various consumers of the
3051 * attribute code have raised their hands.
3053 * Currently this is used only by UFS Extended Attributes.
3056 extattrctl(struct extattrctl_args *uap)
3058 struct thread *td = curthread;
3059 struct nameidata nd;
3063 NDINIT(&nd, LOOKUP, FOLLOW, UIO_USERSPACE, SCARG(uap, path), td);
3064 if ((error = namei(&nd)) != 0)
3066 mp = nd.ni_vp->v_mount;
3068 return (VFS_EXTATTRCTL(mp, SCARG(uap, cmd), SCARG(uap, attrname),
3069 SCARG(uap, arg), td));
3073 * Syscall to set a named extended attribute on a file or directory.
3074 * Accepts attribute name, and a uio structure pointing to the data to set.
3075 * The uio is consumed in the style of writev(). The real work happens
3076 * in VOP_SETEXTATTR().
3079 extattr_set_file(struct extattr_set_file_args *uap)
3081 struct thread *td = curthread;
3082 struct proc *p = td->td_proc;
3083 struct nameidata nd;
3085 struct iovec *iov, *needfree = NULL, aiov[UIO_SMALLIOV];
3086 char attrname[EXTATTR_MAXNAMELEN];
3090 error = copyin(SCARG(uap, attrname), attrname, EXTATTR_MAXNAMELEN);
3093 NDINIT(&nd, LOOKUP, FOLLOW | LOCKLEAF, UIO_USERSPACE,
3094 SCARG(uap, path), td);
3095 if ((error = namei(&nd)) != 0)
3097 iovlen = uap->iovcnt * sizeof(struct iovec);
3098 if (uap->iovcnt > UIO_SMALLIOV) {
3099 if (uap->iovcnt > UIO_MAXIOV) {
3103 MALLOC(iov, struct iovec *, iovlen, M_IOV, M_WAITOK);
3108 auio.uio_iovcnt = uap->iovcnt;
3109 auio.uio_rw = UIO_WRITE;
3110 auio.uio_segflg = UIO_USERSPACE;
3112 auio.uio_offset = 0;
3113 if ((error = copyin((caddr_t)uap->iovp, (caddr_t)iov, iovlen)))
3116 for (i = 0; i < uap->iovcnt; i++) {
3117 if (iov->iov_len > INT_MAX - auio.uio_resid) {
3121 auio.uio_resid += iov->iov_len;
3124 cnt = auio.uio_resid;
3125 error = VOP_SETEXTATTR(nd.ni_vp, attrname, &auio, p->p_ucred, td);
3126 cnt -= auio.uio_resid;
3127 p->p_retval[0] = cnt;
3130 FREE(needfree, M_IOV);
3136 * Syscall to get a named extended attribute on a file or directory.
3137 * Accepts attribute name, and a uio structure pointing to a buffer for the
3138 * data. The uio is consumed in the style of readv(). The real work
3139 * happens in VOP_GETEXTATTR();
3142 extattr_get_file(struct extattr_get_file_args *uap)
3144 struct thread *td = curthread;
3145 struct proc *p = td->td_proc;
3146 struct nameidata nd;
3148 struct iovec *iov, *needfree, aiov[UIO_SMALLIOV];
3149 char attrname[EXTATTR_MAXNAMELEN];
3153 error = copyin(SCARG(uap, attrname), attrname, EXTATTR_MAXNAMELEN);
3156 NDINIT(&nd, LOOKUP, FOLLOW | LOCKLEAF, UIO_USERSPACE,
3157 SCARG(uap, path), td);
3158 if ((error = namei(&nd)) != 0)
3160 iovlen = uap->iovcnt * sizeof (struct iovec);
3161 if (uap->iovcnt > UIO_SMALLIOV) {
3162 if (uap->iovcnt > UIO_MAXIOV) {
3166 MALLOC(iov, struct iovec *, iovlen, M_IOV, M_WAITOK);
3173 auio.uio_iovcnt = uap->iovcnt;
3174 auio.uio_rw = UIO_READ;
3175 auio.uio_segflg = UIO_USERSPACE;
3177 auio.uio_offset = 0;
3178 if ((error = copyin((caddr_t)uap->iovp, (caddr_t)iov, iovlen)))
3181 for (i = 0; i < uap->iovcnt; i++) {
3182 if (iov->iov_len > INT_MAX - auio.uio_resid) {
3186 auio.uio_resid += iov->iov_len;
3189 cnt = auio.uio_resid;
3190 error = VOP_GETEXTATTR(nd.ni_vp, attrname, &auio, p->p_ucred, td);
3191 cnt -= auio.uio_resid;
3192 p->p_retval[0] = cnt;
3195 FREE(needfree, M_IOV);
3201 * Syscall to delete a named extended attribute from a file or directory.
3202 * Accepts attribute name. The real work happens in VOP_SETEXTATTR().
3205 extattr_delete_file(struct extattr_delete_file_args *uap)
3207 struct thread *td = curthread;
3208 struct proc *p = td->td_proc;
3209 struct nameidata nd;
3210 char attrname[EXTATTR_MAXNAMELEN];
3213 error = copyin(SCARG(uap, attrname), attrname, EXTATTR_MAXNAMELEN);
3216 NDINIT(&nd, LOOKUP, FOLLOW | LOCKLEAF, UIO_USERSPACE,
3217 SCARG(uap, path), td);
3218 if ((error = namei(&nd)) != 0)
3220 error = VOP_SETEXTATTR(nd.ni_vp, attrname, NULL, p->p_ucred, td);