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.13 2003/07/30 00:19: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.
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;
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 mount *mp, *nmp;
693 long count, maxcount, error;
695 maxcount = SCARG(uap, bufsize) / sizeof(struct statfs);
696 sfsp = (caddr_t)SCARG(uap, buf);
698 lwkt_gettoken(&mountlist_token);
699 for (mp = TAILQ_FIRST(&mountlist); mp != NULL; mp = nmp) {
700 if (vfs_busy(mp, LK_NOWAIT, &mountlist_token, td)) {
701 nmp = TAILQ_NEXT(mp, mnt_list);
704 if (sfsp && count < maxcount) {
707 * If MNT_NOWAIT or MNT_LAZY is specified, do not
708 * refresh the fsstat cache. MNT_NOWAIT or MNT_LAZY
709 * overrides MNT_WAIT.
711 if (((SCARG(uap, flags) & (MNT_LAZY|MNT_NOWAIT)) == 0 ||
712 (SCARG(uap, flags) & MNT_WAIT)) &&
713 (error = VFS_STATFS(mp, sp, td))) {
714 lwkt_gettoken(&mountlist_token);
715 nmp = TAILQ_NEXT(mp, mnt_list);
719 sp->f_flags = mp->mnt_flag & MNT_VISFLAGMASK;
720 error = copyout((caddr_t)sp, sfsp, sizeof(*sp));
728 lwkt_gettoken(&mountlist_token);
729 nmp = TAILQ_NEXT(mp, mnt_list);
732 lwkt_reltoken(&mountlist_token);
733 if (sfsp && count > maxcount)
734 uap->sysmsg_result = maxcount;
736 uap->sysmsg_result = count;
741 * fchdir_args(int fd)
743 * Change current working directory to a given file descriptor.
747 fchdir(struct fchdir_args *uap)
749 struct thread *td = curthread;
750 struct proc *p = td->td_proc;
751 struct filedesc *fdp = p->p_fd;
752 struct vnode *vp, *tdp;
757 if ((error = getvnode(fdp, SCARG(uap, fd), &fp)) != 0)
759 vp = (struct vnode *)fp->f_data;
761 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY, td);
762 if (vp->v_type != VDIR)
765 error = VOP_ACCESS(vp, VEXEC, p->p_ucred, td);
766 while (!error && (mp = vp->v_mountedhere) != NULL) {
767 if (vfs_busy(mp, 0, 0, td))
769 error = VFS_ROOT(mp, &tdp);
780 VOP_UNLOCK(vp, 0, td);
787 * chdir_args(char *path)
789 * Change current working directory (``.'').
793 chdir(struct chdir_args *uap)
795 struct thread *td = curthread;
796 struct proc *p = td->td_proc;
797 struct filedesc *fdp = p->p_fd;
801 NDINIT(&nd, LOOKUP, FOLLOW | LOCKLEAF, UIO_USERSPACE,
802 SCARG(uap, path), td);
803 if ((error = change_dir(&nd, td)) != 0)
805 NDFREE(&nd, NDF_ONLY_PNBUF);
807 fdp->fd_cdir = nd.ni_vp;
812 * Helper function for raised chroot(2) security function: Refuse if
813 * any filedescriptors are open directories.
816 chroot_refuse_vdir_fds(fdp)
817 struct filedesc *fdp;
824 for (fd = 0; fd < fdp->fd_nfiles ; fd++) {
825 error = getvnode(fdp, fd, &fp);
828 vp = (struct vnode *)fp->f_data;
829 if (vp->v_type != VDIR)
837 * This sysctl determines if we will allow a process to chroot(2) if it
838 * has a directory open:
839 * 0: disallowed for all processes.
840 * 1: allowed for processes that were not already chroot(2)'ed.
841 * 2: allowed for all processes.
844 static int chroot_allow_open_directories = 1;
846 SYSCTL_INT(_kern, OID_AUTO, chroot_allow_open_directories, CTLFLAG_RW,
847 &chroot_allow_open_directories, 0, "");
850 * chroot_args(char *path)
852 * Change notion of root (``/'') directory.
856 chroot(struct chroot_args *uap)
858 struct thread *td = curthread;
859 struct proc *p = td->td_proc;
860 struct filedesc *fdp = p->p_fd;
865 error = suser_cred(p->p_ucred, PRISON_ROOT);
868 if (chroot_allow_open_directories == 0 ||
869 (chroot_allow_open_directories == 1 && fdp->fd_rdir != rootvnode))
870 error = chroot_refuse_vdir_fds(fdp);
873 NDINIT(&nd, LOOKUP, FOLLOW | LOCKLEAF, UIO_USERSPACE,
874 SCARG(uap, path), td);
875 if ((error = change_dir(&nd, td)) != 0)
877 NDFREE(&nd, NDF_ONLY_PNBUF);
879 fdp->fd_rdir = nd.ni_vp;
881 fdp->fd_jdir = nd.ni_vp;
888 * Common routine for chroot and chdir.
891 change_dir(struct nameidata *ndp, struct thread *td)
900 if (vp->v_type != VDIR)
903 error = VOP_ACCESS(vp, VEXEC, ndp->ni_cnd.cn_cred, td);
907 VOP_UNLOCK(vp, 0, td);
912 * open_args(char *path, int flags, int mode)
914 * Check permissions, allocate an open file structure,
915 * and call the device open routine if any.
918 open(struct open_args *uap)
920 struct thread *td = curthread;
921 struct proc *p = td->td_proc;
922 struct filedesc *fdp = p->p_fd;
925 int cmode, flags, oflags;
927 int type, indx, error;
931 oflags = SCARG(uap, flags);
932 if ((oflags & O_ACCMODE) == O_ACCMODE)
934 flags = FFLAGS(oflags);
935 error = falloc(p, &nfp, &indx);
939 cmode = ((SCARG(uap, mode) &~ fdp->fd_cmask) & ALLPERMS) &~ S_ISTXT;
940 NDINIT(&nd, LOOKUP, FOLLOW, UIO_USERSPACE, SCARG(uap, path), td);
941 p->p_dupfd = -indx - 1; /* XXX check for fdopen */
943 * Bump the ref count to prevent another process from closing
944 * the descriptor while we are blocked in vn_open()
947 error = vn_open(&nd, flags, cmode);
950 * release our own reference
955 * handle special fdopen() case. bleh. dupfdopen() is
956 * responsible for dropping the old contents of ofiles[indx]
959 if ((error == ENODEV || error == ENXIO) &&
960 p->p_dupfd >= 0 && /* XXX from fdopen */
962 dupfdopen(fdp, indx, p->p_dupfd, flags, error)) == 0) {
963 uap->sysmsg_result = indx;
967 * Clean up the descriptor, but only if another thread hadn't
968 * replaced or closed it.
970 if (fdp->fd_ofiles[indx] == fp) {
971 fdp->fd_ofiles[indx] = NULL;
975 if (error == ERESTART)
980 NDFREE(&nd, NDF_ONLY_PNBUF);
984 * There should be 2 references on the file, one from the descriptor
985 * table, and one for us.
987 * Handle the case where someone closed the file (via its file
988 * descriptor) while we were blocked. The end result should look
989 * like opening the file succeeded but it was immediately closed.
991 if (fp->f_count == 1) {
992 KASSERT(fdp->fd_ofiles[indx] != fp,
993 ("Open file descriptor lost all refs"));
994 VOP_UNLOCK(vp, 0, td);
995 vn_close(vp, flags & FMASK, td);
997 uap->sysmsg_result = indx;
1001 fp->f_data = (caddr_t)vp;
1002 fp->f_flag = flags & FMASK;
1004 fp->f_type = (vp->v_type == VFIFO ? DTYPE_FIFO : DTYPE_VNODE);
1005 if (flags & (O_EXLOCK | O_SHLOCK)) {
1006 lf.l_whence = SEEK_SET;
1009 if (flags & O_EXLOCK)
1010 lf.l_type = F_WRLCK;
1012 lf.l_type = F_RDLCK;
1014 if ((flags & FNONBLOCK) == 0)
1016 VOP_UNLOCK(vp, 0, td);
1017 if ((error = VOP_ADVLOCK(vp, (caddr_t)fp, F_SETLK, &lf, type)) != 0) {
1019 * lock request failed. Normally close the descriptor
1020 * but handle the case where someone might have dup()d
1021 * it when we weren't looking. One reference is
1022 * owned by the descriptor array, the other by us.
1024 if (fdp->fd_ofiles[indx] == fp) {
1025 fdp->fd_ofiles[indx] = NULL;
1031 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY, td);
1032 fp->f_flag |= FHASLOCK;
1034 /* assert that vn_open created a backing object if one is needed */
1035 KASSERT(!vn_canvmio(vp) || VOP_GETVOBJECT(vp, NULL) == 0,
1036 ("open: vmio vnode has no backing object after vn_open"));
1037 VOP_UNLOCK(vp, 0, td);
1040 * release our private reference, leaving the one associated with the
1041 * descriptor table intact.
1044 uap->sysmsg_result = indx;
1050 * ocreat(char *path, int mode)
1055 ocreat(struct ocreat_args *uap)
1057 struct open_args /* {
1058 syscallarg(char *) path;
1059 syscallarg(int) flags;
1060 syscallarg(int) mode;
1063 SCARG(&nuap, path) = SCARG(uap, path);
1064 SCARG(&nuap, mode) = SCARG(uap, mode);
1065 SCARG(&nuap, flags) = O_WRONLY | O_CREAT | O_TRUNC;
1066 return (open(&nuap));
1068 #endif /* COMPAT_43 */
1071 * mknod_args(char *path, int mode, int dev)
1073 * Create a special file.
1077 mknod(struct mknod_args *uap)
1079 struct thread *td = curthread;
1080 struct proc *p = td->td_proc;
1085 struct nameidata nd;
1089 switch (SCARG(uap, mode) & S_IFMT) {
1095 error = suser_cred(p->p_ucred, PRISON_ROOT);
1101 NDINIT(&nd, CREATE, LOCKPARENT, UIO_USERSPACE, SCARG(uap, path), td);
1102 if ((error = namei(&nd)) != 0)
1109 vattr.va_mode = (SCARG(uap, mode) & ALLPERMS) &~ p->p_fd->fd_cmask;
1110 vattr.va_rdev = SCARG(uap, dev);
1113 switch (SCARG(uap, mode) & S_IFMT) {
1114 case S_IFMT: /* used by badsect to flag bad sectors */
1115 vattr.va_type = VBAD;
1118 vattr.va_type = VCHR;
1121 vattr.va_type = VBLK;
1132 VOP_LEASE(nd.ni_dvp, td, p->p_ucred, LEASE_WRITE);
1134 error = VOP_WHITEOUT(nd.ni_dvp, &nd.ni_cnd, CREATE);
1136 error = VOP_MKNOD(nd.ni_dvp, &nd.ni_vp,
1137 &nd.ni_cnd, &vattr);
1141 NDFREE(&nd, NDF_ONLY_PNBUF);
1144 NDFREE(&nd, NDF_ONLY_PNBUF);
1145 if (nd.ni_dvp == vp)
1152 ASSERT_VOP_UNLOCKED(nd.ni_dvp, "mknod");
1153 ASSERT_VOP_UNLOCKED(nd.ni_vp, "mknod");
1158 * mkfifo_args(char *path, int mode)
1160 * Create a named pipe.
1164 mkfifo(struct mkfifo_args *uap)
1166 struct thread *td = curthread;
1167 struct proc *p = td->td_proc;
1170 struct nameidata nd;
1173 NDINIT(&nd, CREATE, LOCKPARENT, UIO_USERSPACE, SCARG(uap, path), td);
1174 if ((error = namei(&nd)) != 0)
1176 if (nd.ni_vp != NULL) {
1177 NDFREE(&nd, NDF_ONLY_PNBUF);
1178 if (nd.ni_dvp == nd.ni_vp)
1186 vattr.va_type = VFIFO;
1187 vattr.va_mode = (SCARG(uap, mode) & ALLPERMS) &~ p->p_fd->fd_cmask;
1188 VOP_LEASE(nd.ni_dvp, td, p->p_ucred, LEASE_WRITE);
1189 error = VOP_MKNOD(nd.ni_dvp, &nd.ni_vp, &nd.ni_cnd, &vattr);
1192 NDFREE(&nd, NDF_ONLY_PNBUF);
1198 * link_args(char *path, char *link)
1200 * Make a hard file link.
1204 link(struct link_args *uap)
1206 struct thread *td = curthread;
1207 struct proc *p = td->td_proc;
1209 struct nameidata nd;
1213 NDINIT(&nd, LOOKUP, FOLLOW|NOOBJ, UIO_USERSPACE, SCARG(uap, path), td);
1214 if ((error = namei(&nd)) != 0)
1216 NDFREE(&nd, NDF_ONLY_PNBUF);
1218 if (vp->v_type == VDIR)
1219 error = EPERM; /* POSIX */
1221 NDINIT(&nd, CREATE, LOCKPARENT|NOOBJ, UIO_USERSPACE, SCARG(uap, link), td);
1224 if (nd.ni_vp != NULL) {
1229 VOP_LEASE(nd.ni_dvp, td, p->p_ucred,
1231 VOP_LEASE(vp, td, p->p_ucred, LEASE_WRITE);
1232 error = VOP_LINK(nd.ni_dvp, vp, &nd.ni_cnd);
1234 NDFREE(&nd, NDF_ONLY_PNBUF);
1235 if (nd.ni_dvp == nd.ni_vp)
1242 ASSERT_VOP_UNLOCKED(nd.ni_dvp, "link");
1243 ASSERT_VOP_UNLOCKED(nd.ni_vp, "link");
1248 * symlink(char *path, char *link)
1250 * Make a symbolic link.
1254 symlink(struct symlink_args *uap)
1256 struct thread *td = curthread;
1257 struct proc *p = td->td_proc;
1261 struct nameidata nd;
1263 path = zalloc(namei_zone);
1264 if ((error = copyinstr(SCARG(uap, path), path, MAXPATHLEN, NULL)) != 0)
1267 NDINIT(&nd, CREATE, LOCKPARENT|NOOBJ, UIO_USERSPACE, SCARG(uap, link), td);
1268 if ((error = namei(&nd)) != 0)
1271 NDFREE(&nd, NDF_ONLY_PNBUF);
1272 if (nd.ni_dvp == nd.ni_vp)
1281 vattr.va_mode = ACCESSPERMS &~ p->p_fd->fd_cmask;
1282 VOP_LEASE(nd.ni_dvp, td, p->p_ucred, LEASE_WRITE);
1283 error = VOP_SYMLINK(nd.ni_dvp, &nd.ni_vp, &nd.ni_cnd, &vattr, path);
1284 NDFREE(&nd, NDF_ONLY_PNBUF);
1288 ASSERT_VOP_UNLOCKED(nd.ni_dvp, "symlink");
1289 ASSERT_VOP_UNLOCKED(nd.ni_vp, "symlink");
1291 zfree(namei_zone, path);
1296 * undelete_args(char *path)
1298 * Delete a whiteout from the filesystem.
1302 undelete(struct undelete_args *uap)
1304 struct thread *td = curthread;
1305 struct proc *p = td->td_proc;
1307 struct nameidata nd;
1310 NDINIT(&nd, DELETE, LOCKPARENT|DOWHITEOUT, UIO_USERSPACE,
1311 SCARG(uap, path), td);
1316 if (nd.ni_vp != NULLVP || !(nd.ni_cnd.cn_flags & ISWHITEOUT)) {
1317 NDFREE(&nd, NDF_ONLY_PNBUF);
1318 if (nd.ni_dvp == nd.ni_vp)
1327 VOP_LEASE(nd.ni_dvp, td, p->p_ucred, LEASE_WRITE);
1328 error = VOP_WHITEOUT(nd.ni_dvp, &nd.ni_cnd, DELETE);
1329 NDFREE(&nd, NDF_ONLY_PNBUF);
1331 ASSERT_VOP_UNLOCKED(nd.ni_dvp, "undelete");
1332 ASSERT_VOP_UNLOCKED(nd.ni_vp, "undelete");
1337 * unlink_args(char *path)
1339 * Delete a name from the filesystem.
1342 unlink(struct unlink_args *uap)
1344 struct thread *td = curthread;
1345 struct proc *p = td->td_proc;
1348 struct nameidata nd;
1351 NDINIT(&nd, DELETE, LOCKPARENT, UIO_USERSPACE, SCARG(uap, path), td);
1352 if ((error = namei(&nd)) != 0)
1355 VOP_LEASE(vp, td, p->p_ucred, LEASE_WRITE);
1356 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY, td);
1358 if (vp->v_type == VDIR)
1359 error = EPERM; /* POSIX */
1362 * The root of a mounted filesystem cannot be deleted.
1364 * XXX: can this only be a VDIR case?
1366 if (vp->v_flag & VROOT)
1371 VOP_LEASE(nd.ni_dvp, td, p->p_ucred, LEASE_WRITE);
1372 error = VOP_REMOVE(nd.ni_dvp, vp, &nd.ni_cnd);
1374 NDFREE(&nd, NDF_ONLY_PNBUF);
1375 if (nd.ni_dvp == vp)
1381 ASSERT_VOP_UNLOCKED(nd.ni_dvp, "unlink");
1382 ASSERT_VOP_UNLOCKED(nd.ni_vp, "unlink");
1387 * lseek_args(int fd, int pad, off_t offset, int whence)
1389 * Reposition read/write file offset.
1392 lseek(struct lseek_args *uap)
1394 struct thread *td = curthread;
1395 struct proc *p = td->td_proc;
1396 struct filedesc *fdp = p->p_fd;
1401 if ((u_int)SCARG(uap, fd) >= fdp->fd_nfiles ||
1402 (fp = fdp->fd_ofiles[SCARG(uap, fd)]) == NULL)
1404 if (fp->f_type != DTYPE_VNODE)
1406 switch (SCARG(uap, whence)) {
1408 fp->f_offset += SCARG(uap, offset);
1411 error=VOP_GETATTR((struct vnode *)fp->f_data, &vattr, td);
1414 fp->f_offset = SCARG(uap, offset) + vattr.va_size;
1417 fp->f_offset = SCARG(uap, offset);
1422 uap->sysmsg_offset = fp->f_offset;
1426 #if defined(COMPAT_43) || defined(COMPAT_SUNOS)
1428 * Reposition read/write file offset.
1430 * olseek_args(int fd, long offset, int whence)
1433 olseek(struct olseek_args *uap)
1435 struct lseek_args /* {
1437 syscallarg(int) pad;
1438 syscallarg(off_t) offset;
1439 syscallarg(int) whence;
1443 SCARG(&nuap, fd) = SCARG(uap, fd);
1444 SCARG(&nuap, offset) = SCARG(uap, offset);
1445 SCARG(&nuap, whence) = SCARG(uap, whence);
1446 error = lseek(&nuap);
1449 #endif /* COMPAT_43 */
1452 * access_args(char *path, int flags)
1454 * Check access permissions.
1457 access(struct access_args *uap)
1459 struct thread *td = curthread;
1460 struct proc *p = td->td_proc;
1461 struct ucred *cred, *tmpcred;
1464 struct nameidata nd;
1468 * Create and modify a temporary credential instead of one that
1469 * is potentially shared. This could also mess up socket
1470 * buffer accounting which can run in an interrupt context.
1472 tmpcred = crdup(cred);
1473 tmpcred->cr_uid = p->p_ucred->cr_ruid;
1474 tmpcred->cr_groups[0] = p->p_ucred->cr_rgid;
1475 p->p_ucred = tmpcred;
1476 NDINIT(&nd, LOOKUP, FOLLOW | LOCKLEAF | NOOBJ, UIO_USERSPACE,
1477 SCARG(uap, path), td);
1478 if ((error = namei(&nd)) != 0)
1482 /* Flags == 0 means only check for existence. */
1483 if (SCARG(uap, flags)) {
1485 if (SCARG(uap, flags) & R_OK)
1487 if (SCARG(uap, flags) & W_OK)
1489 if (SCARG(uap, flags) & X_OK)
1491 if ((flags & VWRITE) == 0 || (error = vn_writechk(vp)) == 0)
1492 error = VOP_ACCESS(vp, flags, tmpcred, td);
1494 NDFREE(&nd, NDF_ONLY_PNBUF);
1502 #if defined(COMPAT_43) || defined(COMPAT_SUNOS)
1504 * ostat_args(char *path, struct ostat *ub)
1506 * Get file status; this version follows links.
1510 ostat(struct ostat_args *uap)
1512 struct thread *td = curthread;
1516 struct nameidata nd;
1518 NDINIT(&nd, LOOKUP, FOLLOW | LOCKLEAF | NOOBJ, UIO_USERSPACE,
1519 SCARG(uap, path), td);
1520 if ((error = namei(&nd)) != 0)
1522 NDFREE(&nd, NDF_ONLY_PNBUF);
1523 error = vn_stat(nd.ni_vp, &sb, td);
1528 error = copyout((caddr_t)&osb, (caddr_t)SCARG(uap, ub), sizeof (osb));
1533 * olstat_args(char *path, struct ostat *ub)
1535 * Get file status; this version does not follow links.
1539 olstat(struct olstat_args *uap)
1541 struct thread *td = curthread;
1546 struct nameidata nd;
1548 NDINIT(&nd, LOOKUP, NOFOLLOW | LOCKLEAF | NOOBJ, UIO_USERSPACE,
1549 SCARG(uap, path), td);
1550 if ((error = namei(&nd)) != 0)
1553 error = vn_stat(vp, &sb, td);
1554 NDFREE(&nd, NDF_ONLY_PNBUF);
1559 error = copyout((caddr_t)&osb, (caddr_t)SCARG(uap, ub), sizeof (osb));
1564 * Convert from an old to a new stat structure.
1571 ost->st_dev = st->st_dev;
1572 ost->st_ino = st->st_ino;
1573 ost->st_mode = st->st_mode;
1574 ost->st_nlink = st->st_nlink;
1575 ost->st_uid = st->st_uid;
1576 ost->st_gid = st->st_gid;
1577 ost->st_rdev = st->st_rdev;
1578 if (st->st_size < (quad_t)1 << 32)
1579 ost->st_size = st->st_size;
1582 ost->st_atime = st->st_atime;
1583 ost->st_mtime = st->st_mtime;
1584 ost->st_ctime = st->st_ctime;
1585 ost->st_blksize = st->st_blksize;
1586 ost->st_blocks = st->st_blocks;
1587 ost->st_flags = st->st_flags;
1588 ost->st_gen = st->st_gen;
1590 #endif /* COMPAT_43 || COMPAT_SUNOS */
1593 * stat_args(char *path, struct stat *ub)
1595 * Get file status; this version follows links.
1599 stat(struct stat_args *uap)
1601 struct thread *td = curthread;
1604 struct nameidata nd;
1606 NDINIT(&nd, LOOKUP, FOLLOW | LOCKLEAF | NOOBJ, UIO_USERSPACE,
1607 SCARG(uap, path), td);
1608 if ((error = namei(&nd)) != 0)
1610 error = vn_stat(nd.ni_vp, &sb, td);
1611 NDFREE(&nd, NDF_ONLY_PNBUF);
1615 error = copyout((caddr_t)&sb, (caddr_t)SCARG(uap, ub), sizeof (sb));
1620 * lstat_args(char *path, struct stat *ub)
1622 * Get file status; this version does not follow links.
1626 lstat(struct lstat_args *uap)
1628 struct thread *td = curthread;
1632 struct nameidata nd;
1634 NDINIT(&nd, LOOKUP, NOFOLLOW | LOCKLEAF | NOOBJ, UIO_USERSPACE,
1635 SCARG(uap, path), td);
1636 if ((error = namei(&nd)) != 0)
1639 error = vn_stat(vp, &sb, td);
1640 NDFREE(&nd, NDF_ONLY_PNBUF);
1644 error = copyout((caddr_t)&sb, (caddr_t)SCARG(uap, ub), sizeof (sb));
1653 nsb->st_dev = sb->st_dev;
1654 nsb->st_ino = sb->st_ino;
1655 nsb->st_mode = sb->st_mode;
1656 nsb->st_nlink = sb->st_nlink;
1657 nsb->st_uid = sb->st_uid;
1658 nsb->st_gid = sb->st_gid;
1659 nsb->st_rdev = sb->st_rdev;
1660 nsb->st_atimespec = sb->st_atimespec;
1661 nsb->st_mtimespec = sb->st_mtimespec;
1662 nsb->st_ctimespec = sb->st_ctimespec;
1663 nsb->st_size = sb->st_size;
1664 nsb->st_blocks = sb->st_blocks;
1665 nsb->st_blksize = sb->st_blksize;
1666 nsb->st_flags = sb->st_flags;
1667 nsb->st_gen = sb->st_gen;
1668 nsb->st_qspare[0] = sb->st_qspare[0];
1669 nsb->st_qspare[1] = sb->st_qspare[1];
1673 * nstat_args(char *path, struct nstat *ub)
1677 nstat(struct nstat_args *uap)
1679 struct thread *td = curthread;
1683 struct nameidata nd;
1685 NDINIT(&nd, LOOKUP, FOLLOW | LOCKLEAF | NOOBJ, UIO_USERSPACE,
1686 SCARG(uap, path), td);
1687 if ((error = namei(&nd)) != 0)
1689 NDFREE(&nd, NDF_ONLY_PNBUF);
1690 error = vn_stat(nd.ni_vp, &sb, td);
1694 cvtnstat(&sb, &nsb);
1695 error = copyout((caddr_t)&nsb, (caddr_t)SCARG(uap, ub), sizeof (nsb));
1700 * lstat_args(char *path, struct stat *ub)
1702 * Get file status; this version does not follow links.
1706 nlstat(struct nlstat_args *uap)
1708 struct thread *td = curthread;
1713 struct nameidata nd;
1715 NDINIT(&nd, LOOKUP, NOFOLLOW | LOCKLEAF | NOOBJ, UIO_USERSPACE,
1716 SCARG(uap, path), td);
1717 if ((error = namei(&nd)) != 0)
1720 NDFREE(&nd, NDF_ONLY_PNBUF);
1721 error = vn_stat(vp, &sb, td);
1725 cvtnstat(&sb, &nsb);
1726 error = copyout((caddr_t)&nsb, (caddr_t)SCARG(uap, ub), sizeof (nsb));
1731 * pathconf_Args(char *path, int name)
1733 * Get configurable pathname variables.
1737 pathconf(struct pathconf_args *uap)
1739 struct thread *td = curthread;
1741 struct nameidata nd;
1743 NDINIT(&nd, LOOKUP, FOLLOW | LOCKLEAF | NOOBJ, UIO_USERSPACE,
1744 SCARG(uap, path), td);
1745 if ((error = namei(&nd)) != 0)
1747 NDFREE(&nd, NDF_ONLY_PNBUF);
1748 error = VOP_PATHCONF(nd.ni_vp, SCARG(uap, name), uap->sysmsg_fds);
1754 * readlink_args(char *path, char *buf, int count)
1756 * Return target name of a symbolic link.
1760 readlink(struct readlink_args *uap)
1762 struct thread *td = curthread;
1763 struct proc *p = td->td_proc;
1768 struct nameidata nd;
1770 NDINIT(&nd, LOOKUP, NOFOLLOW | LOCKLEAF | NOOBJ, UIO_USERSPACE,
1771 SCARG(uap, path), td);
1772 if ((error = namei(&nd)) != 0)
1774 NDFREE(&nd, NDF_ONLY_PNBUF);
1776 if (vp->v_type != VLNK)
1779 aiov.iov_base = SCARG(uap, buf);
1780 aiov.iov_len = SCARG(uap, count);
1781 auio.uio_iov = &aiov;
1782 auio.uio_iovcnt = 1;
1783 auio.uio_offset = 0;
1784 auio.uio_rw = UIO_READ;
1785 auio.uio_segflg = UIO_USERSPACE;
1787 auio.uio_resid = SCARG(uap, count);
1788 error = VOP_READLINK(vp, &auio, p->p_ucred);
1791 uap->sysmsg_result = SCARG(uap, count) - auio.uio_resid;
1796 setfflags(struct vnode *vp, int flags)
1798 struct thread *td = curthread;
1799 struct proc *p = td->td_proc;
1804 * Prevent non-root users from setting flags on devices. When
1805 * a device is reused, users can retain ownership of the device
1806 * if they are allowed to set flags and programs assume that
1807 * chown can't fail when done as root.
1809 if ((vp->v_type == VCHR || vp->v_type == VBLK) &&
1810 ((error = suser_cred(p->p_ucred, PRISON_ROOT)) != 0))
1813 VOP_LEASE(vp, td, p->p_ucred, LEASE_WRITE);
1814 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY, td);
1816 vattr.va_flags = flags;
1817 error = VOP_SETATTR(vp, &vattr, p->p_ucred, td);
1818 VOP_UNLOCK(vp, 0, td);
1823 * chflags(char *path, int flags)
1825 * Change flags of a file given a path name.
1829 chflags(struct chflags_args *uap)
1831 struct thread *td = curthread;
1833 struct nameidata nd;
1835 NDINIT(&nd, LOOKUP, FOLLOW, UIO_USERSPACE, SCARG(uap, path), td);
1836 if ((error = namei(&nd)) != 0)
1838 NDFREE(&nd, NDF_ONLY_PNBUF);
1839 error = setfflags(nd.ni_vp, SCARG(uap, flags));
1845 * fchflags_args(int fd, int flags)
1847 * Change flags of a file given a file descriptor.
1851 fchflags(struct fchflags_args *uap)
1853 struct thread *td = curthread;
1854 struct proc *p = td->td_proc;
1858 if ((error = getvnode(p->p_fd, SCARG(uap, fd), &fp)) != 0)
1860 return setfflags((struct vnode *) fp->f_data, SCARG(uap, flags));
1864 setfmode(struct vnode *vp, int mode)
1866 struct thread *td = curthread;
1867 struct proc *p = td->td_proc;
1871 VOP_LEASE(vp, td, p->p_ucred, LEASE_WRITE);
1872 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY, td);
1874 vattr.va_mode = mode & ALLPERMS;
1875 error = VOP_SETATTR(vp, &vattr, p->p_ucred, td);
1876 VOP_UNLOCK(vp, 0, td);
1881 * chmod_args(char *path, int mode)
1883 * Change mode of a file given path name.
1887 chmod(struct chmod_args *uap)
1889 struct thread *td = curthread;
1891 struct nameidata nd;
1893 NDINIT(&nd, LOOKUP, FOLLOW, UIO_USERSPACE, SCARG(uap, path), td);
1894 if ((error = namei(&nd)) != 0)
1896 NDFREE(&nd, NDF_ONLY_PNBUF);
1897 error = setfmode(nd.ni_vp, SCARG(uap, mode));
1903 * lchmod_args(char *path, int mode)
1905 * Change mode of a file given path name (don't follow links.)
1909 lchmod(struct lchmod_args *uap)
1911 struct thread *td = curthread;
1913 struct nameidata nd;
1915 NDINIT(&nd, LOOKUP, NOFOLLOW, UIO_USERSPACE, SCARG(uap, path), td);
1916 if ((error = namei(&nd)) != 0)
1918 NDFREE(&nd, NDF_ONLY_PNBUF);
1919 error = setfmode(nd.ni_vp, SCARG(uap, mode));
1925 * fchmod_args(int fd, int mode)
1927 * Change mode of a file given a file descriptor.
1931 fchmod(struct fchmod_args *uap)
1933 struct thread *td = curthread;
1934 struct proc *p = td->td_proc;
1938 if ((error = getvnode(p->p_fd, SCARG(uap, fd), &fp)) != 0)
1940 return setfmode((struct vnode *)fp->f_data, SCARG(uap, mode));
1944 setfown(struct vnode *vp, uid_t uid, gid_t gid)
1946 struct thread *td = curthread;
1947 struct proc *p = td->td_proc;
1951 VOP_LEASE(vp, td, p->p_ucred, LEASE_WRITE);
1952 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY, td);
1956 error = VOP_SETATTR(vp, &vattr, p->p_ucred, td);
1957 VOP_UNLOCK(vp, 0, td);
1962 * chown(char *path, int uid, int gid)
1964 * Set ownership given a path name.
1968 chown(struct chown_args *uap)
1970 struct thread *td = curthread;
1972 struct nameidata nd;
1974 NDINIT(&nd, LOOKUP, FOLLOW, UIO_USERSPACE, SCARG(uap, path), td);
1975 if ((error = namei(&nd)) != 0)
1977 NDFREE(&nd, NDF_ONLY_PNBUF);
1978 error = setfown(nd.ni_vp, SCARG(uap, uid), SCARG(uap, gid));
1984 * lchown_args(char *path, int uid, int gid)
1986 * Set ownership given a path name, do not cross symlinks.
1990 lchown(struct lchown_args *uap)
1992 struct thread *td = curthread;
1994 struct nameidata nd;
1996 NDINIT(&nd, LOOKUP, NOFOLLOW, UIO_USERSPACE, SCARG(uap, path), td);
1997 if ((error = namei(&nd)) != 0)
1999 NDFREE(&nd, NDF_ONLY_PNBUF);
2000 error = setfown(nd.ni_vp, SCARG(uap, uid), SCARG(uap, gid));
2006 * fchown_args(int fd, int uid, int gid)
2008 * Set ownership given a file descriptor.
2012 fchown(struct fchown_args *uap)
2014 struct thread *td = curthread;
2015 struct proc *p = td->td_proc;
2019 if ((error = getvnode(p->p_fd, SCARG(uap, fd), &fp)) != 0)
2021 return setfown((struct vnode *)fp->f_data,
2022 SCARG(uap, uid), SCARG(uap, gid));
2026 getutimes(const struct timeval *usrtvp, struct timespec *tsp)
2028 struct timeval tv[2];
2031 if (usrtvp == NULL) {
2033 TIMEVAL_TO_TIMESPEC(&tv[0], &tsp[0]);
2036 if ((error = copyin(usrtvp, tv, sizeof (tv))) != 0)
2038 TIMEVAL_TO_TIMESPEC(&tv[0], &tsp[0]);
2039 TIMEVAL_TO_TIMESPEC(&tv[1], &tsp[1]);
2045 setutimes(struct vnode *vp, const struct timespec *ts, int nullflag)
2047 struct thread *td = curthread;
2048 struct proc *p = td->td_proc;
2052 VOP_LEASE(vp, td, p->p_ucred, LEASE_WRITE);
2053 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY, td);
2055 vattr.va_atime = ts[0];
2056 vattr.va_mtime = ts[1];
2058 vattr.va_vaflags |= VA_UTIMES_NULL;
2059 error = VOP_SETATTR(vp, &vattr, p->p_ucred, td);
2060 VOP_UNLOCK(vp, 0, td);
2065 * utimes_args(char *path, struct timeval *tptr)
2067 * Set the access and modification times of a file.
2071 utimes(struct utimes_args *uap)
2073 struct thread *td = curthread;
2074 struct timespec ts[2];
2075 struct timeval *usrtvp;
2077 struct nameidata nd;
2079 usrtvp = SCARG(uap, tptr);
2080 if ((error = getutimes(usrtvp, ts)) != 0)
2082 NDINIT(&nd, LOOKUP, FOLLOW, UIO_USERSPACE, SCARG(uap, path), td);
2083 if ((error = namei(&nd)) != 0)
2085 NDFREE(&nd, NDF_ONLY_PNBUF);
2086 error = setutimes(nd.ni_vp, ts, usrtvp == NULL);
2092 * lutimes_args(char *path, struct timeval *tptr)
2094 * Set the access and modification times of a file.
2098 lutimes(struct lutimes_args *uap)
2100 struct thread *td = curthread;
2101 struct timespec ts[2];
2102 struct timeval *usrtvp;
2104 struct nameidata nd;
2106 usrtvp = SCARG(uap, tptr);
2107 if ((error = getutimes(usrtvp, ts)) != 0)
2109 NDINIT(&nd, LOOKUP, NOFOLLOW, UIO_USERSPACE, SCARG(uap, path), td);
2110 if ((error = namei(&nd)) != 0)
2112 NDFREE(&nd, NDF_ONLY_PNBUF);
2113 error = setutimes(nd.ni_vp, ts, usrtvp == NULL);
2119 * futimes_args(int fd, struct timeval *tptr)
2121 * Set the access and modification times of a file.
2125 futimes(struct futimes_args *uap)
2127 struct thread *td = curthread;
2128 struct proc *p = td->td_proc;
2129 struct timespec ts[2];
2131 struct timeval *usrtvp;
2134 usrtvp = SCARG(uap, tptr);
2135 if ((error = getutimes(usrtvp, ts)) != 0)
2137 if ((error = getvnode(p->p_fd, SCARG(uap, fd), &fp)) != 0)
2139 return setutimes((struct vnode *)fp->f_data, ts, usrtvp == NULL);
2143 * truncate(char *path, int pad, off_t length)
2145 * Truncate a file given its path name.
2149 truncate(struct truncate_args *uap)
2151 struct thread *td = curthread;
2152 struct proc *p = td->td_proc;
2156 struct nameidata nd;
2158 if (uap->length < 0)
2160 NDINIT(&nd, LOOKUP, FOLLOW, UIO_USERSPACE, SCARG(uap, path), td);
2161 if ((error = namei(&nd)) != 0)
2164 NDFREE(&nd, NDF_ONLY_PNBUF);
2165 VOP_LEASE(vp, td, p->p_ucred, LEASE_WRITE);
2166 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY, td);
2167 if (vp->v_type == VDIR)
2169 else if ((error = vn_writechk(vp)) == 0 &&
2170 (error = VOP_ACCESS(vp, VWRITE, p->p_ucred, td)) == 0) {
2172 vattr.va_size = SCARG(uap, length);
2173 error = VOP_SETATTR(vp, &vattr, p->p_ucred, td);
2180 * ftruncate_args(int fd, int pad, off_t length)
2182 * Truncate a file given a file descriptor.
2186 ftruncate(struct ftruncate_args *uap)
2188 struct thread *td = curthread;
2189 struct proc *p = td->td_proc;
2195 if (uap->length < 0)
2197 if ((error = getvnode(p->p_fd, SCARG(uap, fd), &fp)) != 0)
2199 if ((fp->f_flag & FWRITE) == 0)
2201 vp = (struct vnode *)fp->f_data;
2202 VOP_LEASE(vp, td, p->p_ucred, LEASE_WRITE);
2203 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY, td);
2204 if (vp->v_type == VDIR)
2206 else if ((error = vn_writechk(vp)) == 0) {
2208 vattr.va_size = SCARG(uap, length);
2209 error = VOP_SETATTR(vp, &vattr, fp->f_cred, td);
2211 VOP_UNLOCK(vp, 0, td);
2215 #if defined(COMPAT_43) || defined(COMPAT_SUNOS)
2217 * otruncate_args(char *path, long length)
2219 * Truncate a file given its path name.
2223 otruncate(struct otruncate_args *uap)
2225 struct truncate_args /* {
2226 syscallarg(char *) path;
2227 syscallarg(int) pad;
2228 syscallarg(off_t) length;
2231 SCARG(&nuap, path) = SCARG(uap, path);
2232 SCARG(&nuap, length) = SCARG(uap, length);
2233 return (truncate(&nuap));
2237 * oftruncate_args(int fd, long length)
2239 * Truncate a file given a file descriptor.
2243 oftruncate(struct oftruncate_args *uap)
2245 struct ftruncate_args /* {
2247 syscallarg(int) pad;
2248 syscallarg(off_t) length;
2251 SCARG(&nuap, fd) = SCARG(uap, fd);
2252 SCARG(&nuap, length) = SCARG(uap, length);
2253 return (ftruncate(&nuap));
2255 #endif /* COMPAT_43 || COMPAT_SUNOS */
2260 * Sync an open file.
2264 fsync(struct fsync_args *uap)
2266 struct thread *td = curthread;
2267 struct proc *p = td->td_proc;
2273 if ((error = getvnode(p->p_fd, SCARG(uap, fd), &fp)) != 0)
2275 vp = (struct vnode *)fp->f_data;
2276 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY, td);
2277 if (VOP_GETVOBJECT(vp, &obj) == 0)
2278 vm_object_page_clean(obj, 0, 0, 0);
2279 if ((error = VOP_FSYNC(vp, MNT_WAIT, td)) == 0 &&
2280 vp->v_mount && (vp->v_mount->mnt_flag & MNT_SOFTDEP) &&
2282 error = (*bioops.io_fsync)(vp);
2283 VOP_UNLOCK(vp, 0, td);
2288 * rename_args(char *from, char *to)
2290 * Rename files. Source and destination must either both be directories,
2291 * or both not be directories. If target is a directory, it must be empty.
2295 rename(struct rename_args *uap)
2297 struct thread *td = curthread;
2298 struct proc *p = td->td_proc;
2299 struct vnode *tvp, *fvp, *tdvp;
2300 struct nameidata fromnd, tond;
2304 NDINIT(&fromnd, DELETE, WANTPARENT | SAVESTART, UIO_USERSPACE,
2305 SCARG(uap, from), td);
2306 if ((error = namei(&fromnd)) != 0)
2309 NDINIT(&tond, RENAME, LOCKPARENT | LOCKLEAF | NOCACHE | SAVESTART | NOOBJ,
2310 UIO_USERSPACE, SCARG(uap, to), td);
2311 if (fromnd.ni_vp->v_type == VDIR)
2312 tond.ni_cnd.cn_flags |= WILLBEDIR;
2313 if ((error = namei(&tond)) != 0) {
2314 /* Translate error code for rename("dir1", "dir2/."). */
2315 if (error == EISDIR && fvp->v_type == VDIR)
2317 NDFREE(&fromnd, NDF_ONLY_PNBUF);
2318 vrele(fromnd.ni_dvp);
2325 if (fvp->v_type == VDIR && tvp->v_type != VDIR) {
2328 } else if (fvp->v_type != VDIR && tvp->v_type == VDIR) {
2336 * If the source is the same as the destination (that is, if they
2337 * are links to the same vnode), then there is nothing to do.
2343 VOP_LEASE(tdvp, td, p->p_ucred, LEASE_WRITE);
2344 if (fromnd.ni_dvp != tdvp) {
2345 VOP_LEASE(fromnd.ni_dvp, td, p->p_ucred, LEASE_WRITE);
2348 VOP_LEASE(tvp, td, p->p_ucred, LEASE_WRITE);
2350 error = VOP_RENAME(fromnd.ni_dvp, fromnd.ni_vp, &fromnd.ni_cnd,
2351 tond.ni_dvp, tond.ni_vp, &tond.ni_cnd);
2352 NDFREE(&fromnd, NDF_ONLY_PNBUF);
2353 NDFREE(&tond, NDF_ONLY_PNBUF);
2355 NDFREE(&fromnd, NDF_ONLY_PNBUF);
2356 NDFREE(&tond, NDF_ONLY_PNBUF);
2363 vrele(fromnd.ni_dvp);
2366 vrele(tond.ni_startdir);
2367 ASSERT_VOP_UNLOCKED(fromnd.ni_dvp, "rename");
2368 ASSERT_VOP_UNLOCKED(fromnd.ni_vp, "rename");
2369 ASSERT_VOP_UNLOCKED(tond.ni_dvp, "rename");
2370 ASSERT_VOP_UNLOCKED(tond.ni_vp, "rename");
2372 if (fromnd.ni_startdir)
2373 vrele(fromnd.ni_startdir);
2380 * mkdir_args(char *path, int mode)
2382 * Make a directory file.
2386 mkdir(struct mkdir_args *uap)
2388 struct thread *td = curthread;
2389 struct proc *p = td->td_proc;
2393 struct nameidata nd;
2396 NDINIT(&nd, CREATE, LOCKPARENT, UIO_USERSPACE, SCARG(uap, path), td);
2397 nd.ni_cnd.cn_flags |= WILLBEDIR;
2398 if ((error = namei(&nd)) != 0)
2402 NDFREE(&nd, NDF_ONLY_PNBUF);
2403 if (nd.ni_dvp == vp)
2411 vattr.va_type = VDIR;
2412 vattr.va_mode = (SCARG(uap, mode) & ACCESSPERMS) &~ p->p_fd->fd_cmask;
2413 VOP_LEASE(nd.ni_dvp, td, p->p_ucred, LEASE_WRITE);
2414 error = VOP_MKDIR(nd.ni_dvp, &nd.ni_vp, &nd.ni_cnd, &vattr);
2415 NDFREE(&nd, NDF_ONLY_PNBUF);
2419 ASSERT_VOP_UNLOCKED(nd.ni_dvp, "mkdir");
2420 ASSERT_VOP_UNLOCKED(nd.ni_vp, "mkdir");
2425 * rmdir_args(char *path)
2427 * Remove a directory file.
2431 rmdir(struct rmdir_args *uap)
2433 struct thread *td = curthread;
2434 struct proc *p = td->td_proc;
2437 struct nameidata nd;
2440 NDINIT(&nd, DELETE, LOCKPARENT | LOCKLEAF, UIO_USERSPACE,
2441 SCARG(uap, path), td);
2442 if ((error = namei(&nd)) != 0)
2445 if (vp->v_type != VDIR) {
2450 * No rmdir "." please.
2452 if (nd.ni_dvp == vp) {
2457 * The root of a mounted filesystem cannot be deleted.
2459 if (vp->v_flag & VROOT)
2462 VOP_LEASE(nd.ni_dvp, td, p->p_ucred, LEASE_WRITE);
2463 VOP_LEASE(vp, td, p->p_ucred, LEASE_WRITE);
2464 error = VOP_RMDIR(nd.ni_dvp, nd.ni_vp, &nd.ni_cnd);
2467 NDFREE(&nd, NDF_ONLY_PNBUF);
2468 if (nd.ni_dvp == vp)
2474 ASSERT_VOP_UNLOCKED(nd.ni_dvp, "rmdir");
2475 ASSERT_VOP_UNLOCKED(nd.ni_vp, "rmdir");
2481 * ogetdirentries_args(int fd, char *buf, u_int count, long *basep)
2483 * Read a block of directory entries in a file system independent format.
2486 ogetdirentries(struct ogetdirentries_args *uap)
2488 struct thread *td = curthread;
2489 struct proc *p = td->td_proc;
2492 struct uio auio, kuio;
2493 struct iovec aiov, kiov;
2494 struct dirent *dp, *edp;
2496 int error, eofflag, readcnt;
2499 /* XXX arbitrary sanity limit on `count'. */
2500 if (SCARG(uap, count) > 64 * 1024)
2502 if ((error = getvnode(p->p_fd, SCARG(uap, fd), &fp)) != 0)
2504 if ((fp->f_flag & FREAD) == 0)
2506 vp = (struct vnode *)fp->f_data;
2508 if (vp->v_type != VDIR)
2510 aiov.iov_base = SCARG(uap, buf);
2511 aiov.iov_len = SCARG(uap, count);
2512 auio.uio_iov = &aiov;
2513 auio.uio_iovcnt = 1;
2514 auio.uio_rw = UIO_READ;
2515 auio.uio_segflg = UIO_USERSPACE;
2517 auio.uio_resid = SCARG(uap, count);
2518 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY, td);
2519 loff = auio.uio_offset = fp->f_offset;
2520 # if (BYTE_ORDER != LITTLE_ENDIAN)
2521 if (vp->v_mount->mnt_maxsymlinklen <= 0) {
2522 error = VOP_READDIR(vp, &auio, fp->f_cred, &eofflag,
2524 fp->f_offset = auio.uio_offset;
2529 kuio.uio_iov = &kiov;
2530 kuio.uio_segflg = UIO_SYSSPACE;
2531 kiov.iov_len = SCARG(uap, count);
2532 MALLOC(dirbuf, caddr_t, SCARG(uap, count), M_TEMP, M_WAITOK);
2533 kiov.iov_base = dirbuf;
2534 error = VOP_READDIR(vp, &kuio, fp->f_cred, &eofflag,
2536 fp->f_offset = kuio.uio_offset;
2538 readcnt = SCARG(uap, count) - kuio.uio_resid;
2539 edp = (struct dirent *)&dirbuf[readcnt];
2540 for (dp = (struct dirent *)dirbuf; dp < edp; ) {
2541 # if (BYTE_ORDER == LITTLE_ENDIAN)
2543 * The expected low byte of
2544 * dp->d_namlen is our dp->d_type.
2545 * The high MBZ byte of dp->d_namlen
2546 * is our dp->d_namlen.
2548 dp->d_type = dp->d_namlen;
2552 * The dp->d_type is the high byte
2553 * of the expected dp->d_namlen,
2554 * so must be zero'ed.
2558 if (dp->d_reclen > 0) {
2559 dp = (struct dirent *)
2560 ((char *)dp + dp->d_reclen);
2567 error = uiomove(dirbuf, readcnt, &auio);
2569 FREE(dirbuf, M_TEMP);
2571 VOP_UNLOCK(vp, 0, td);
2574 if (SCARG(uap, count) == auio.uio_resid) {
2575 if (union_dircheckp) {
2576 error = union_dircheckp(td, &vp, fp);
2582 if ((vp->v_flag & VROOT) &&
2583 (vp->v_mount->mnt_flag & MNT_UNION)) {
2584 struct vnode *tvp = vp;
2585 vp = vp->v_mount->mnt_vnodecovered;
2587 fp->f_data = (caddr_t) vp;
2593 error = copyout((caddr_t)&loff, (caddr_t)SCARG(uap, basep),
2595 uap->sysmsg_result = SCARG(uap, count) - auio.uio_resid;
2598 #endif /* COMPAT_43 */
2601 * getdirentries_args(int fd, char *buf, u_int conut, long *basep)
2603 * Read a block of directory entries in a file system independent format.
2606 getdirentries(struct getdirentries_args *uap)
2608 struct thread *td = curthread;
2609 struct proc *p = td->td_proc;
2617 if ((error = getvnode(p->p_fd, SCARG(uap, fd), &fp)) != 0)
2619 if ((fp->f_flag & FREAD) == 0)
2621 vp = (struct vnode *)fp->f_data;
2623 if (vp->v_type != VDIR)
2625 aiov.iov_base = SCARG(uap, buf);
2626 aiov.iov_len = SCARG(uap, count);
2627 auio.uio_iov = &aiov;
2628 auio.uio_iovcnt = 1;
2629 auio.uio_rw = UIO_READ;
2630 auio.uio_segflg = UIO_USERSPACE;
2632 auio.uio_resid = SCARG(uap, count);
2633 /* vn_lock(vp, LK_SHARED | LK_RETRY, td); */
2634 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY, td);
2635 loff = auio.uio_offset = fp->f_offset;
2636 error = VOP_READDIR(vp, &auio, fp->f_cred, &eofflag, NULL, NULL);
2637 fp->f_offset = auio.uio_offset;
2638 VOP_UNLOCK(vp, 0, td);
2641 if (SCARG(uap, count) == auio.uio_resid) {
2642 if (union_dircheckp) {
2643 error = union_dircheckp(td, &vp, fp);
2649 if ((vp->v_flag & VROOT) &&
2650 (vp->v_mount->mnt_flag & MNT_UNION)) {
2651 struct vnode *tvp = vp;
2652 vp = vp->v_mount->mnt_vnodecovered;
2654 fp->f_data = (caddr_t) vp;
2660 if (SCARG(uap, basep) != NULL) {
2661 error = copyout((caddr_t)&loff, (caddr_t)SCARG(uap, basep),
2664 uap->sysmsg_result = SCARG(uap, count) - auio.uio_resid;
2669 * getdents_args(int fd, char *buf, size_t count)
2672 getdents(struct getdents_args *uap)
2674 struct getdirentries_args ap;
2678 ap.count = uap->count;
2680 return getdirentries(&ap);
2684 * umask(int newmask)
2686 * Set the mode mask for creation of filesystem nodes.
2691 umask(struct umask_args *uap)
2693 struct thread *td = curthread;
2694 struct proc *p = td->td_proc;
2695 struct filedesc *fdp;
2698 uap->sysmsg_result = fdp->fd_cmask;
2699 fdp->fd_cmask = SCARG(uap, newmask) & ALLPERMS;
2704 * revoke(char *path)
2706 * Void all references to file by ripping underlying filesystem
2711 revoke(struct revoke_args *uap)
2713 struct thread *td = curthread;
2714 struct proc *p = td->td_proc;
2718 struct nameidata nd;
2720 NDINIT(&nd, LOOKUP, FOLLOW, UIO_USERSPACE, SCARG(uap, path), td);
2721 if ((error = namei(&nd)) != 0)
2724 NDFREE(&nd, NDF_ONLY_PNBUF);
2725 if (vp->v_type != VCHR && vp->v_type != VBLK) {
2729 if ((error = VOP_GETATTR(vp, &vattr, td)) != 0)
2731 if (p->p_ucred->cr_uid != vattr.va_uid &&
2732 (error = suser_cred(p->p_ucred, PRISON_ROOT)))
2735 VOP_REVOKE(vp, REVOKEALL);
2742 * Convert a user file descriptor to a kernel file entry.
2745 getvnode(struct filedesc *fdp, int fd, struct file **fpp)
2749 if ((u_int)fd >= fdp->fd_nfiles ||
2750 (fp = fdp->fd_ofiles[fd]) == NULL)
2752 if (fp->f_type != DTYPE_VNODE && fp->f_type != DTYPE_FIFO)
2758 * getfh_args(char *fname, fhandle_t *fhp)
2760 * Get (NFS) file handle
2763 getfh(struct getfh_args *uap)
2765 struct thread *td = curthread;
2766 struct nameidata nd;
2772 * Must be super user
2777 NDINIT(&nd, LOOKUP, FOLLOW | LOCKLEAF, UIO_USERSPACE, uap->fname, td);
2781 NDFREE(&nd, NDF_ONLY_PNBUF);
2783 bzero(&fh, sizeof(fh));
2784 fh.fh_fsid = vp->v_mount->mnt_stat.f_fsid;
2785 error = VFS_VPTOFH(vp, &fh.fh_fid);
2789 error = copyout(&fh, uap->fhp, sizeof (fh));
2794 * fhopen_args(const struct fhandle *u_fhp, int flags)
2796 * syscall for the rpc.lockd to use to translate a NFS file handle into
2797 * an open descriptor.
2799 * warning: do not remove the suser() call or this becomes one giant
2803 fhopen(struct fhopen_args *uap)
2805 struct thread *td = curthread;
2806 struct proc *p = td->td_proc;
2811 struct vattr *vap = &vat;
2814 struct filedesc *fdp = p->p_fd;
2815 int fmode, mode, error, type;
2820 * Must be super user
2826 fmode = FFLAGS(SCARG(uap, flags));
2827 /* why not allow a non-read/write open for our lockd? */
2828 if (((fmode & (FREAD | FWRITE)) == 0) || (fmode & O_CREAT))
2830 error = copyin(SCARG(uap,u_fhp), &fhp, sizeof(fhp));
2833 /* find the mount point */
2834 mp = vfs_getvfs(&fhp.fh_fsid);
2837 /* now give me my vnode, it gets returned to me locked */
2838 error = VFS_FHTOVP(mp, &fhp.fh_fid, &vp);
2842 * from now on we have to make sure not
2843 * to forget about the vnode
2844 * any error that causes an abort must vput(vp)
2845 * just set error = err and 'goto bad;'.
2851 if (vp->v_type == VLNK) {
2855 if (vp->v_type == VSOCK) {
2860 if (fmode & (FWRITE | O_TRUNC)) {
2861 if (vp->v_type == VDIR) {
2865 error = vn_writechk(vp);
2873 error = VOP_ACCESS(vp, mode, p->p_ucred, td);
2877 if (fmode & O_TRUNC) {
2878 VOP_UNLOCK(vp, 0, td); /* XXX */
2879 VOP_LEASE(vp, td, p->p_ucred, LEASE_WRITE);
2880 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY, td); /* XXX */
2883 error = VOP_SETATTR(vp, vap, p->p_ucred, td);
2887 error = VOP_OPEN(vp, fmode, p->p_ucred, td);
2891 * Make sure that a VM object is created for VMIO support.
2893 if (vn_canvmio(vp) == TRUE) {
2894 if ((error = vfs_object_create(vp, td)) != 0)
2901 * end of vn_open code
2904 if ((error = falloc(p, &nfp, &indx)) != 0) {
2912 * hold an extra reference to avoid having fp ripped out
2913 * from under us while we block in the lock op.
2916 nfp->f_data = (caddr_t)vp;
2917 nfp->f_flag = fmode & FMASK;
2918 nfp->f_ops = &vnops;
2919 nfp->f_type = DTYPE_VNODE;
2920 if (fmode & (O_EXLOCK | O_SHLOCK)) {
2921 lf.l_whence = SEEK_SET;
2924 if (fmode & O_EXLOCK)
2925 lf.l_type = F_WRLCK;
2927 lf.l_type = F_RDLCK;
2929 if ((fmode & FNONBLOCK) == 0)
2931 VOP_UNLOCK(vp, 0, td);
2932 if ((error = VOP_ADVLOCK(vp, (caddr_t)fp, F_SETLK, &lf, type)) != 0) {
2934 * lock request failed. Normally close the descriptor
2935 * but handle the case where someone might have dup()d
2936 * or close()d it when we weren't looking.
2938 if (fdp->fd_ofiles[indx] == fp) {
2939 fdp->fd_ofiles[indx] = NULL;
2944 * release our private reference.
2949 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY, td);
2950 fp->f_flag |= FHASLOCK;
2952 if ((vp->v_type == VREG) && (VOP_GETVOBJECT(vp, NULL) != 0))
2953 vfs_object_create(vp, td);
2955 VOP_UNLOCK(vp, 0, td);
2957 uap->sysmsg_result = indx;
2966 * fhstat_args(struct fhandle *u_fhp, struct stat *sb)
2969 fhstat(struct fhstat_args *uap)
2971 struct thread *td = curthread;
2979 * Must be super user
2985 error = copyin(SCARG(uap, u_fhp), &fh, sizeof(fhandle_t));
2989 if ((mp = vfs_getvfs(&fh.fh_fsid)) == NULL)
2991 if ((error = VFS_FHTOVP(mp, &fh.fh_fid, &vp)))
2993 error = vn_stat(vp, &sb, td);
2997 error = copyout(&sb, SCARG(uap, sb), sizeof(sb));
3002 * fhstatfs_args(struct fhandle *u_fhp, struct statfs *buf)
3005 fhstatfs(struct fhstatfs_args *uap)
3007 struct thread *td = curthread;
3016 * Must be super user
3018 if ((error = suser(td)))
3021 if ((error = copyin(SCARG(uap, u_fhp), &fh, sizeof(fhandle_t))) != 0)
3024 if ((mp = vfs_getvfs(&fh.fh_fsid)) == NULL)
3026 if ((error = VFS_FHTOVP(mp, &fh.fh_fid, &vp)))
3031 if ((error = VFS_STATFS(mp, sp, td)) != 0)
3033 sp->f_flags = mp->mnt_flag & MNT_VISFLAGMASK;
3035 bcopy((caddr_t)sp, (caddr_t)&sb, sizeof(sb));
3036 sb.f_fsid.val[0] = sb.f_fsid.val[1] = 0;
3039 return (copyout(sp, SCARG(uap, buf), sizeof(*sp)));
3043 * Syscall to push extended attribute configuration information into the
3044 * VFS. Accepts a path, which it converts to a mountpoint, as well as
3045 * a command (int cmd), and attribute name and misc data. For now, the
3046 * attribute name is left in userspace for consumption by the VFS_op.
3047 * It will probably be changed to be copied into sysspace by the
3048 * syscall in the future, once issues with various consumers of the
3049 * attribute code have raised their hands.
3051 * Currently this is used only by UFS Extended Attributes.
3054 extattrctl(struct extattrctl_args *uap)
3056 struct thread *td = curthread;
3057 struct nameidata nd;
3061 NDINIT(&nd, LOOKUP, FOLLOW, UIO_USERSPACE, SCARG(uap, path), td);
3062 if ((error = namei(&nd)) != 0)
3064 mp = nd.ni_vp->v_mount;
3066 return (VFS_EXTATTRCTL(mp, SCARG(uap, cmd), SCARG(uap, attrname),
3067 SCARG(uap, arg), td));
3071 * Syscall to set a named extended attribute on a file or directory.
3072 * Accepts attribute name, and a uio structure pointing to the data to set.
3073 * The uio is consumed in the style of writev(). The real work happens
3074 * in VOP_SETEXTATTR().
3077 extattr_set_file(struct extattr_set_file_args *uap)
3079 struct thread *td = curthread;
3080 struct proc *p = td->td_proc;
3081 struct nameidata nd;
3083 struct iovec *iov, *needfree = NULL, aiov[UIO_SMALLIOV];
3084 char attrname[EXTATTR_MAXNAMELEN];
3088 error = copyin(SCARG(uap, attrname), attrname, EXTATTR_MAXNAMELEN);
3091 NDINIT(&nd, LOOKUP, FOLLOW | LOCKLEAF, UIO_USERSPACE,
3092 SCARG(uap, path), td);
3093 if ((error = namei(&nd)) != 0)
3095 iovlen = uap->iovcnt * sizeof(struct iovec);
3096 if (uap->iovcnt > UIO_SMALLIOV) {
3097 if (uap->iovcnt > UIO_MAXIOV) {
3101 MALLOC(iov, struct iovec *, iovlen, M_IOV, M_WAITOK);
3106 auio.uio_iovcnt = uap->iovcnt;
3107 auio.uio_rw = UIO_WRITE;
3108 auio.uio_segflg = UIO_USERSPACE;
3110 auio.uio_offset = 0;
3111 if ((error = copyin((caddr_t)uap->iovp, (caddr_t)iov, iovlen)))
3114 for (i = 0; i < uap->iovcnt; i++) {
3115 if (iov->iov_len > INT_MAX - auio.uio_resid) {
3119 auio.uio_resid += iov->iov_len;
3122 cnt = auio.uio_resid;
3123 error = VOP_SETEXTATTR(nd.ni_vp, attrname, &auio, p->p_ucred, td);
3124 cnt -= auio.uio_resid;
3125 uap->sysmsg_result = cnt;
3128 FREE(needfree, M_IOV);
3134 * Syscall to get a named extended attribute on a file or directory.
3135 * Accepts attribute name, and a uio structure pointing to a buffer for the
3136 * data. The uio is consumed in the style of readv(). The real work
3137 * happens in VOP_GETEXTATTR();
3140 extattr_get_file(struct extattr_get_file_args *uap)
3142 struct thread *td = curthread;
3143 struct proc *p = td->td_proc;
3144 struct nameidata nd;
3146 struct iovec *iov, *needfree, aiov[UIO_SMALLIOV];
3147 char attrname[EXTATTR_MAXNAMELEN];
3151 error = copyin(SCARG(uap, attrname), attrname, EXTATTR_MAXNAMELEN);
3154 NDINIT(&nd, LOOKUP, FOLLOW | LOCKLEAF, UIO_USERSPACE,
3155 SCARG(uap, path), td);
3156 if ((error = namei(&nd)) != 0)
3158 iovlen = uap->iovcnt * sizeof (struct iovec);
3159 if (uap->iovcnt > UIO_SMALLIOV) {
3160 if (uap->iovcnt > UIO_MAXIOV) {
3164 MALLOC(iov, struct iovec *, iovlen, M_IOV, M_WAITOK);
3171 auio.uio_iovcnt = uap->iovcnt;
3172 auio.uio_rw = UIO_READ;
3173 auio.uio_segflg = UIO_USERSPACE;
3175 auio.uio_offset = 0;
3176 if ((error = copyin((caddr_t)uap->iovp, (caddr_t)iov, iovlen)))
3179 for (i = 0; i < uap->iovcnt; i++) {
3180 if (iov->iov_len > INT_MAX - auio.uio_resid) {
3184 auio.uio_resid += iov->iov_len;
3187 cnt = auio.uio_resid;
3188 error = VOP_GETEXTATTR(nd.ni_vp, attrname, &auio, p->p_ucred, td);
3189 cnt -= auio.uio_resid;
3190 uap->sysmsg_result = cnt;
3193 FREE(needfree, M_IOV);
3199 * Syscall to delete a named extended attribute from a file or directory.
3200 * Accepts attribute name. The real work happens in VOP_SETEXTATTR().
3203 extattr_delete_file(struct extattr_delete_file_args *uap)
3205 struct thread *td = curthread;
3206 struct proc *p = td->td_proc;
3207 struct nameidata nd;
3208 char attrname[EXTATTR_MAXNAMELEN];
3211 error = copyin(SCARG(uap, attrname), attrname, EXTATTR_MAXNAMELEN);
3214 NDINIT(&nd, LOOKUP, FOLLOW | LOCKLEAF, UIO_USERSPACE,
3215 SCARG(uap, path), td);
3216 if ((error = namei(&nd)) != 0)
3218 error = VOP_SETEXTATTR(nd.ni_vp, attrname, NULL, p->p_ucred, td);