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.21 2003/10/21 01:05:09 daver 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>
65 #include <sys/kern_syscall.h>
67 #include <machine/limits.h>
68 #include <vfs/union/union.h>
69 #include <sys/sysctl.h>
71 #include <vm/vm_object.h>
72 #include <vm/vm_zone.h>
73 #include <vm/vm_page.h>
75 #include <sys/file2.h>
77 static int change_dir (struct nameidata *ndp, struct thread *td);
78 static void checkdirs (struct vnode *olddp);
79 static int chroot_refuse_vdir_fds (struct filedesc *fdp);
80 static int getutimes (const struct timeval *, struct timespec *);
81 static int setfown (struct vnode *, uid_t, gid_t);
82 static int setfmode (struct vnode *, int);
83 static int setfflags (struct vnode *, int);
84 static int setutimes (struct vnode *, const struct timespec *, int);
85 static int usermount = 0; /* if 1, non-root can mount fs. */
87 int (*union_dircheckp) (struct thread *, struct vnode **, struct file *);
89 SYSCTL_INT(_vfs, OID_AUTO, usermount, CTLFLAG_RW, &usermount, 0, "");
92 * Virtual File System System Calls
96 * Mount a file system.
99 * mount_args(char *type, char *path, int flags, caddr_t data)
103 mount(struct mount_args *uap)
105 struct thread *td = curthread;
106 struct proc *p = td->td_proc;
109 struct vfsconf *vfsp;
110 int error, flag = 0, flag2 = 0;
116 char fstypename[MFSNAMELEN];
118 if (usermount == 0 && (error = suser(td)))
121 * Do not allow NFS export by non-root users.
123 if (SCARG(uap, flags) & MNT_EXPORTED) {
129 * Silently enforce MNT_NOSUID and MNT_NODEV for non-root users
132 SCARG(uap, flags) |= MNT_NOSUID | MNT_NODEV;
134 * Get vnode to be covered
136 NDINIT(&nd, NAMEI_LOOKUP, CNP_FOLLOW | CNP_LOCKLEAF, UIO_USERSPACE,
137 SCARG(uap, path), td);
138 if ((error = namei(&nd)) != 0)
140 NDFREE(&nd, NDF_ONLY_PNBUF);
142 if (SCARG(uap, flags) & MNT_UPDATE) {
143 if ((vp->v_flag & VROOT) == 0) {
149 flag2 = mp->mnt_kern_flag;
151 * We only allow the filesystem to be reloaded if it
152 * is currently mounted read-only.
154 if ((SCARG(uap, flags) & MNT_RELOAD) &&
155 ((mp->mnt_flag & MNT_RDONLY) == 0)) {
157 return (EOPNOTSUPP); /* Needs translation */
160 * Only root, or the user that did the original mount is
161 * permitted to update it.
163 if (mp->mnt_stat.f_owner != p->p_ucred->cr_uid &&
164 (error = suser(td))) {
168 if (vfs_busy(mp, LK_NOWAIT, 0, td)) {
172 lwkt_gettoken(&vp->v_interlock);
173 if ((vp->v_flag & VMOUNT) != 0 ||
174 vp->v_mountedhere != NULL) {
175 lwkt_reltoken(&vp->v_interlock);
180 vp->v_flag |= VMOUNT;
181 lwkt_reltoken(&vp->v_interlock);
183 SCARG(uap, flags) & (MNT_RELOAD | MNT_FORCE | MNT_UPDATE);
184 VOP_UNLOCK(vp, 0, td);
188 * If the user is not root, ensure that they own the directory
189 * onto which we are attempting to mount.
191 if ((error = VOP_GETATTR(vp, &va, td)) ||
192 (va.va_uid != p->p_ucred->cr_uid &&
193 (error = suser(td)))) {
197 if ((error = vinvalbuf(vp, V_SAVE, td, 0, 0)) != 0) {
201 if (vp->v_type != VDIR) {
207 * Historically filesystem types were identified by number. If we
208 * get an integer for the filesystem type instead of a string, we
209 * check to see if it matches one of the historic filesystem types.
211 fstypenum = (uintptr_t)SCARG(uap, type);
212 if (fstypenum < maxvfsconf) {
213 for (vfsp = vfsconf; vfsp; vfsp = vfsp->vfc_next)
214 if (vfsp->vfc_typenum == fstypenum)
220 strncpy(fstypename, vfsp->vfc_name, MFSNAMELEN);
222 #endif /* COMPAT_43 */
223 if ((error = copyinstr(SCARG(uap, type), fstypename, MFSNAMELEN, NULL)) != 0) {
227 for (vfsp = vfsconf; vfsp; vfsp = vfsp->vfc_next)
228 if (!strcmp(vfsp->vfc_name, fstypename))
233 /* Only load modules for root (very important!) */
234 if ((error = suser(td)) != 0) {
238 error = linker_load_file(fstypename, &lf);
239 if (error || lf == NULL) {
246 /* lookup again, see if the VFS was loaded */
247 for (vfsp = vfsconf; vfsp; vfsp = vfsp->vfc_next)
248 if (!strcmp(vfsp->vfc_name, fstypename))
252 linker_file_unload(lf);
257 lwkt_gettoken(&vp->v_interlock);
258 if ((vp->v_flag & VMOUNT) != 0 ||
259 vp->v_mountedhere != NULL) {
260 lwkt_reltoken(&vp->v_interlock);
264 vp->v_flag |= VMOUNT;
265 lwkt_reltoken(&vp->v_interlock);
268 * Allocate and initialize the filesystem.
270 mp = malloc(sizeof(struct mount), M_MOUNT, M_WAITOK);
271 bzero((char *)mp, (u_long)sizeof(struct mount));
272 TAILQ_INIT(&mp->mnt_nvnodelist);
273 TAILQ_INIT(&mp->mnt_reservedvnlist);
274 mp->mnt_nvnodelistsize = 0;
275 lockinit(&mp->mnt_lock, 0, "vfslock", 0, LK_NOPAUSE);
276 (void)vfs_busy(mp, LK_NOWAIT, 0, td);
277 mp->mnt_op = vfsp->vfc_vfsops;
279 vfsp->vfc_refcount++;
280 mp->mnt_stat.f_type = vfsp->vfc_typenum;
281 mp->mnt_flag |= vfsp->vfc_flags & MNT_VISFLAGMASK;
282 strncpy(mp->mnt_stat.f_fstypename, vfsp->vfc_name, MFSNAMELEN);
283 mp->mnt_vnodecovered = vp;
284 mp->mnt_stat.f_owner = p->p_ucred->cr_uid;
285 mp->mnt_iosize_max = DFLTPHYS;
286 VOP_UNLOCK(vp, 0, td);
289 * Set the mount level flags.
291 if (SCARG(uap, flags) & MNT_RDONLY)
292 mp->mnt_flag |= MNT_RDONLY;
293 else if (mp->mnt_flag & MNT_RDONLY)
294 mp->mnt_kern_flag |= MNTK_WANTRDWR;
295 mp->mnt_flag &=~ (MNT_NOSUID | MNT_NOEXEC | MNT_NODEV |
296 MNT_SYNCHRONOUS | MNT_UNION | MNT_ASYNC | MNT_NOATIME |
297 MNT_NOSYMFOLLOW | MNT_IGNORE |
298 MNT_NOCLUSTERR | MNT_NOCLUSTERW | MNT_SUIDDIR);
299 mp->mnt_flag |= SCARG(uap, flags) & (MNT_NOSUID | MNT_NOEXEC |
300 MNT_NODEV | MNT_SYNCHRONOUS | MNT_UNION | MNT_ASYNC | MNT_FORCE |
301 MNT_NOSYMFOLLOW | MNT_IGNORE |
302 MNT_NOATIME | MNT_NOCLUSTERR | MNT_NOCLUSTERW | MNT_SUIDDIR);
304 * Mount the filesystem.
305 * XXX The final recipients of VFS_MOUNT just overwrite the ndp they
306 * get. No freeing of cn_pnbuf.
308 error = VFS_MOUNT(mp, SCARG(uap, path), SCARG(uap, data), &nd, td);
309 if (mp->mnt_flag & MNT_UPDATE) {
310 if (mp->mnt_kern_flag & MNTK_WANTRDWR)
311 mp->mnt_flag &= ~MNT_RDONLY;
312 mp->mnt_flag &=~ (MNT_UPDATE | MNT_RELOAD | MNT_FORCE);
313 mp->mnt_kern_flag &=~ MNTK_WANTRDWR;
316 mp->mnt_kern_flag = flag2;
318 if ((mp->mnt_flag & MNT_RDONLY) == 0) {
319 if (mp->mnt_syncer == NULL)
320 error = vfs_allocate_syncvnode(mp);
322 if (mp->mnt_syncer != NULL)
323 vrele(mp->mnt_syncer);
324 mp->mnt_syncer = NULL;
327 lwkt_gettoken(&vp->v_interlock);
328 vp->v_flag &= ~VMOUNT;
329 lwkt_reltoken(&vp->v_interlock);
333 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY, td);
335 * Put the new filesystem on the mount list after root.
339 lwkt_gettoken(&vp->v_interlock);
340 vp->v_flag &= ~VMOUNT;
341 vp->v_mountedhere = mp;
342 lwkt_reltoken(&vp->v_interlock);
343 lwkt_gettoken(&mountlist_token);
344 TAILQ_INSERT_TAIL(&mountlist, mp, mnt_list);
345 lwkt_reltoken(&mountlist_token);
347 VOP_UNLOCK(vp, 0, td);
348 if ((mp->mnt_flag & MNT_RDONLY) == 0)
349 error = vfs_allocate_syncvnode(mp);
351 if ((error = VFS_START(mp, 0, td)) != 0)
354 lwkt_gettoken(&vp->v_interlock);
355 vp->v_flag &= ~VMOUNT;
356 lwkt_reltoken(&vp->v_interlock);
357 mp->mnt_vfc->vfc_refcount--;
359 free((caddr_t)mp, M_MOUNT);
366 * Scan all active processes to see if any of them have a current
367 * or root directory onto which the new filesystem has just been
368 * mounted. If so, replace them with the new mount point.
371 checkdirs(struct vnode *olddp)
373 struct filedesc *fdp;
377 if (olddp->v_usecount == 1)
379 if (VFS_ROOT(olddp->v_mountedhere, &newdp))
380 panic("mount: lost mount");
381 FOREACH_PROC_IN_SYSTEM(p) {
383 if (fdp->fd_cdir == olddp) {
386 fdp->fd_cdir = newdp;
388 if (fdp->fd_rdir == olddp) {
391 fdp->fd_rdir = newdp;
394 if (rootvnode == olddp) {
398 vfs_cache_setroot(rootvnode);
404 * Unmount a file system.
406 * Note: unmount takes a path to the vnode mounted on as argument,
407 * not special file (as before).
410 * umount_args(char *path, int flags)
414 unmount(struct unmount_args *uap)
416 struct thread *td = curthread;
417 struct proc *p = td->td_proc;
424 NDINIT(&nd, NAMEI_LOOKUP, CNP_FOLLOW | CNP_LOCKLEAF, UIO_USERSPACE,
425 SCARG(uap, path), td);
426 if ((error = namei(&nd)) != 0)
429 NDFREE(&nd, NDF_ONLY_PNBUF);
433 * Only root, or the user that did the original mount is
434 * permitted to unmount this filesystem.
436 if ((mp->mnt_stat.f_owner != p->p_ucred->cr_uid) &&
437 (error = suser(td))) {
443 * Don't allow unmounting the root file system.
445 if (mp->mnt_flag & MNT_ROOTFS) {
451 * Must be the root of the filesystem
453 if ((vp->v_flag & VROOT) == 0) {
458 return (dounmount(mp, SCARG(uap, flags), td));
462 * Do the actual file system unmount.
465 dounmount(struct mount *mp, int flags, struct thread *td)
467 struct vnode *coveredvp;
471 lwkt_gettoken(&mountlist_token);
472 if (mp->mnt_kern_flag & MNTK_UNMOUNT) {
473 lwkt_reltoken(&mountlist_token);
476 mp->mnt_kern_flag |= MNTK_UNMOUNT;
477 /* Allow filesystems to detect that a forced unmount is in progress. */
478 if (flags & MNT_FORCE)
479 mp->mnt_kern_flag |= MNTK_UNMOUNTF;
480 error = lockmgr(&mp->mnt_lock, LK_DRAIN | LK_INTERLOCK |
481 ((flags & MNT_FORCE) ? 0 : LK_NOWAIT), &mountlist_token, td);
483 mp->mnt_kern_flag &= ~(MNTK_UNMOUNT | MNTK_UNMOUNTF);
484 if (mp->mnt_kern_flag & MNTK_MWAIT)
489 if (mp->mnt_flag & MNT_EXPUBLIC)
490 vfs_setpublicfs(NULL, NULL, NULL);
492 vfs_msync(mp, MNT_WAIT);
493 async_flag = mp->mnt_flag & MNT_ASYNC;
494 mp->mnt_flag &=~ MNT_ASYNC;
495 cache_purgevfs(mp); /* remove cache entries for this file sys */
496 if (mp->mnt_syncer != NULL)
497 vrele(mp->mnt_syncer);
498 if (((mp->mnt_flag & MNT_RDONLY) ||
499 (error = VFS_SYNC(mp, MNT_WAIT, td)) == 0) ||
501 error = VFS_UNMOUNT(mp, flags, td);
502 lwkt_gettoken(&mountlist_token);
504 if ((mp->mnt_flag & MNT_RDONLY) == 0 && mp->mnt_syncer == NULL)
505 (void) vfs_allocate_syncvnode(mp);
506 mp->mnt_kern_flag &= ~(MNTK_UNMOUNT | MNTK_UNMOUNTF);
507 mp->mnt_flag |= async_flag;
508 lockmgr(&mp->mnt_lock, LK_RELEASE | LK_INTERLOCK | LK_REENABLE,
509 &mountlist_token, td);
510 if (mp->mnt_kern_flag & MNTK_MWAIT)
514 TAILQ_REMOVE(&mountlist, mp, mnt_list);
515 if ((coveredvp = mp->mnt_vnodecovered) != NULLVP) {
516 coveredvp->v_mountedhere = NULL;
519 mp->mnt_vfc->vfc_refcount--;
520 if (!TAILQ_EMPTY(&mp->mnt_nvnodelist))
521 panic("unmount: dangling vnode");
522 lockmgr(&mp->mnt_lock, LK_RELEASE | LK_INTERLOCK, &mountlist_token, td);
523 if (mp->mnt_kern_flag & MNTK_MWAIT)
525 free((caddr_t)mp, M_MOUNT);
530 * Sync each mounted filesystem.
534 static int syncprt = 0;
535 SYSCTL_INT(_debug, OID_AUTO, syncprt, CTLFLAG_RW, &syncprt, 0, "");
540 sync(struct sync_args *uap)
542 struct thread *td = curthread;
543 struct mount *mp, *nmp;
546 lwkt_gettoken(&mountlist_token);
547 for (mp = TAILQ_FIRST(&mountlist); mp != NULL; mp = nmp) {
548 if (vfs_busy(mp, LK_NOWAIT, &mountlist_token, td)) {
549 nmp = TAILQ_NEXT(mp, mnt_list);
552 if ((mp->mnt_flag & MNT_RDONLY) == 0) {
553 asyncflag = mp->mnt_flag & MNT_ASYNC;
554 mp->mnt_flag &= ~MNT_ASYNC;
555 vfs_msync(mp, MNT_NOWAIT);
556 VFS_SYNC(mp, MNT_NOWAIT, td);
557 mp->mnt_flag |= asyncflag;
559 lwkt_gettoken(&mountlist_token);
560 nmp = TAILQ_NEXT(mp, mnt_list);
563 lwkt_reltoken(&mountlist_token);
566 * XXX don't call vfs_bufstats() yet because that routine
567 * was not imported in the Lite2 merge.
572 #endif /* DIAGNOSTIC */
577 /* XXX PRISON: could be per prison flag */
578 static int prison_quotas;
580 SYSCTL_INT(_kern_prison, OID_AUTO, quotas, CTLFLAG_RW, &prison_quotas, 0, "");
584 * quotactl_args(char *path, int fcmd, int uid, caddr_t arg)
586 * Change filesystem quotas.
590 quotactl(struct quotactl_args *uap)
592 struct thread *td = curthread;
593 struct proc *p = td->td_proc;
599 if (p->p_ucred->cr_prison && !prison_quotas)
601 NDINIT(&nd, NAMEI_LOOKUP, CNP_FOLLOW, UIO_USERSPACE,
602 SCARG(uap, path), td);
603 if ((error = namei(&nd)) != 0)
605 mp = nd.ni_vp->v_mount;
606 NDFREE(&nd, NDF_ONLY_PNBUF);
608 return (VFS_QUOTACTL(mp, SCARG(uap, cmd), SCARG(uap, uid),
609 SCARG(uap, arg), td));
613 * statfs_args(char *path, struct statfs *buf)
615 * Get filesystem statistics.
619 statfs(struct statfs_args *uap)
621 struct thread *td = curthread;
628 NDINIT(&nd, NAMEI_LOOKUP, CNP_FOLLOW, UIO_USERSPACE,
629 SCARG(uap, path), td);
630 if ((error = namei(&nd)) != 0)
632 mp = nd.ni_vp->v_mount;
634 NDFREE(&nd, NDF_ONLY_PNBUF);
636 error = VFS_STATFS(mp, sp, td);
639 sp->f_flags = mp->mnt_flag & MNT_VISFLAGMASK;
641 bcopy((caddr_t)sp, (caddr_t)&sb, sizeof(sb));
642 sb.f_fsid.val[0] = sb.f_fsid.val[1] = 0;
645 return (copyout((caddr_t)sp, (caddr_t)SCARG(uap, buf), sizeof(*sp)));
649 * fstatfs_args(int fd, struct statfs *buf)
651 * Get filesystem statistics.
655 fstatfs(struct fstatfs_args *uap)
657 struct thread *td = curthread;
658 struct proc *p = td->td_proc;
666 if ((error = getvnode(p->p_fd, SCARG(uap, fd), &fp)) != 0)
668 mp = ((struct vnode *)fp->f_data)->v_mount;
672 error = VFS_STATFS(mp, sp, td);
675 sp->f_flags = mp->mnt_flag & MNT_VISFLAGMASK;
677 bcopy((caddr_t)sp, (caddr_t)&sb, sizeof(sb));
678 sb.f_fsid.val[0] = sb.f_fsid.val[1] = 0;
681 return (copyout((caddr_t)sp, (caddr_t)SCARG(uap, buf), sizeof(*sp)));
685 * getfsstat_args(struct statfs *buf, long bufsize, int flags)
687 * Get statistics on all filesystems.
691 getfsstat(struct getfsstat_args *uap)
693 struct thread *td = curthread;
694 struct mount *mp, *nmp;
697 long count, maxcount, error;
699 maxcount = SCARG(uap, bufsize) / sizeof(struct statfs);
700 sfsp = (caddr_t)SCARG(uap, buf);
702 lwkt_gettoken(&mountlist_token);
703 for (mp = TAILQ_FIRST(&mountlist); mp != NULL; mp = nmp) {
704 if (vfs_busy(mp, LK_NOWAIT, &mountlist_token, td)) {
705 nmp = TAILQ_NEXT(mp, mnt_list);
708 if (sfsp && count < maxcount) {
711 * If MNT_NOWAIT or MNT_LAZY is specified, do not
712 * refresh the fsstat cache. MNT_NOWAIT or MNT_LAZY
713 * overrides MNT_WAIT.
715 if (((SCARG(uap, flags) & (MNT_LAZY|MNT_NOWAIT)) == 0 ||
716 (SCARG(uap, flags) & MNT_WAIT)) &&
717 (error = VFS_STATFS(mp, sp, td))) {
718 lwkt_gettoken(&mountlist_token);
719 nmp = TAILQ_NEXT(mp, mnt_list);
723 sp->f_flags = mp->mnt_flag & MNT_VISFLAGMASK;
724 error = copyout((caddr_t)sp, sfsp, sizeof(*sp));
732 lwkt_gettoken(&mountlist_token);
733 nmp = TAILQ_NEXT(mp, mnt_list);
736 lwkt_reltoken(&mountlist_token);
737 if (sfsp && count > maxcount)
738 uap->sysmsg_result = maxcount;
740 uap->sysmsg_result = count;
745 * fchdir_args(int fd)
747 * Change current working directory to a given file descriptor.
751 fchdir(struct fchdir_args *uap)
753 struct thread *td = curthread;
754 struct proc *p = td->td_proc;
755 struct filedesc *fdp = p->p_fd;
756 struct vnode *vp, *tdp;
761 if ((error = getvnode(fdp, SCARG(uap, fd), &fp)) != 0)
763 vp = (struct vnode *)fp->f_data;
765 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY, td);
766 if (vp->v_type != VDIR)
769 error = VOP_ACCESS(vp, VEXEC, p->p_ucred, td);
770 while (!error && (mp = vp->v_mountedhere) != NULL) {
771 if (vfs_busy(mp, 0, 0, td))
773 error = VFS_ROOT(mp, &tdp);
784 VOP_UNLOCK(vp, 0, td);
791 * chdir_args(char *path)
793 * Change current working directory (``.'').
797 chdir(struct chdir_args *uap)
799 struct thread *td = curthread;
800 struct proc *p = td->td_proc;
801 struct filedesc *fdp = p->p_fd;
805 NDINIT(&nd, NAMEI_LOOKUP, CNP_FOLLOW | CNP_LOCKLEAF, UIO_USERSPACE,
806 SCARG(uap, path), td);
807 if ((error = change_dir(&nd, td)) != 0)
809 NDFREE(&nd, NDF_ONLY_PNBUF);
811 fdp->fd_cdir = nd.ni_vp;
816 * Helper function for raised chroot(2) security function: Refuse if
817 * any filedescriptors are open directories.
820 chroot_refuse_vdir_fds(fdp)
821 struct filedesc *fdp;
828 for (fd = 0; fd < fdp->fd_nfiles ; fd++) {
829 error = getvnode(fdp, fd, &fp);
832 vp = (struct vnode *)fp->f_data;
833 if (vp->v_type != VDIR)
841 * This sysctl determines if we will allow a process to chroot(2) if it
842 * has a directory open:
843 * 0: disallowed for all processes.
844 * 1: allowed for processes that were not already chroot(2)'ed.
845 * 2: allowed for all processes.
848 static int chroot_allow_open_directories = 1;
850 SYSCTL_INT(_kern, OID_AUTO, chroot_allow_open_directories, CTLFLAG_RW,
851 &chroot_allow_open_directories, 0, "");
854 * chroot_args(char *path)
856 * Change notion of root (``/'') directory.
860 chroot(struct chroot_args *uap)
862 struct thread *td = curthread;
863 struct proc *p = td->td_proc;
864 struct filedesc *fdp = p->p_fd;
869 error = suser_cred(p->p_ucred, PRISON_ROOT);
872 if (chroot_allow_open_directories == 0 ||
873 (chroot_allow_open_directories == 1 && fdp->fd_rdir != rootvnode))
874 error = chroot_refuse_vdir_fds(fdp);
877 NDINIT(&nd, NAMEI_LOOKUP, CNP_FOLLOW | CNP_LOCKLEAF, UIO_USERSPACE,
878 SCARG(uap, path), td);
879 if ((error = change_dir(&nd, td)) != 0)
881 NDFREE(&nd, NDF_ONLY_PNBUF);
883 fdp->fd_rdir = nd.ni_vp;
885 fdp->fd_jdir = nd.ni_vp;
892 * Common routine for chroot and chdir.
895 change_dir(struct nameidata *ndp, struct thread *td)
904 if (vp->v_type != VDIR)
907 error = VOP_ACCESS(vp, VEXEC, ndp->ni_cnd.cn_cred, td);
911 VOP_UNLOCK(vp, 0, td);
916 * open_args(char *path, int flags, int mode)
918 * Check permissions, allocate an open file structure,
919 * and call the device open routine if any.
922 open(struct open_args *uap)
924 struct thread *td = curthread;
925 struct proc *p = td->td_proc;
926 struct filedesc *fdp = p->p_fd;
929 int cmode, flags, oflags;
931 int type, indx, error;
935 oflags = SCARG(uap, flags);
936 if ((oflags & O_ACCMODE) == O_ACCMODE)
938 flags = FFLAGS(oflags);
939 error = falloc(p, &nfp, &indx);
943 cmode = ((SCARG(uap, mode) &~ fdp->fd_cmask) & ALLPERMS) &~ S_ISTXT;
944 NDINIT(&nd, NAMEI_LOOKUP, CNP_FOLLOW, UIO_USERSPACE,
945 SCARG(uap, path), td);
946 p->p_dupfd = -indx - 1; /* XXX check for fdopen */
948 * Bump the ref count to prevent another process from closing
949 * the descriptor while we are blocked in vn_open()
952 error = vn_open(&nd, flags, cmode);
955 * release our own reference
960 * handle special fdopen() case. bleh. dupfdopen() is
961 * responsible for dropping the old contents of ofiles[indx]
964 if ((error == ENODEV || error == ENXIO) &&
965 p->p_dupfd >= 0 && /* XXX from fdopen */
967 dupfdopen(fdp, indx, p->p_dupfd, flags, error)) == 0) {
968 uap->sysmsg_result = indx;
972 * Clean up the descriptor, but only if another thread hadn't
973 * replaced or closed it.
975 if (fdp->fd_ofiles[indx] == fp) {
976 fdp->fd_ofiles[indx] = NULL;
980 if (error == ERESTART)
985 NDFREE(&nd, NDF_ONLY_PNBUF);
989 * There should be 2 references on the file, one from the descriptor
990 * table, and one for us.
992 * Handle the case where someone closed the file (via its file
993 * descriptor) while we were blocked. The end result should look
994 * like opening the file succeeded but it was immediately closed.
996 if (fp->f_count == 1) {
997 KASSERT(fdp->fd_ofiles[indx] != fp,
998 ("Open file descriptor lost all refs"));
999 VOP_UNLOCK(vp, 0, td);
1000 vn_close(vp, flags & FMASK, td);
1002 uap->sysmsg_result = indx;
1006 fp->f_data = (caddr_t)vp;
1007 fp->f_flag = flags & FMASK;
1009 fp->f_type = (vp->v_type == VFIFO ? DTYPE_FIFO : DTYPE_VNODE);
1010 if (flags & (O_EXLOCK | O_SHLOCK)) {
1011 lf.l_whence = SEEK_SET;
1014 if (flags & O_EXLOCK)
1015 lf.l_type = F_WRLCK;
1017 lf.l_type = F_RDLCK;
1019 if ((flags & FNONBLOCK) == 0)
1021 VOP_UNLOCK(vp, 0, td);
1022 if ((error = VOP_ADVLOCK(vp, (caddr_t)fp, F_SETLK, &lf, type)) != 0) {
1024 * lock request failed. Normally close the descriptor
1025 * but handle the case where someone might have dup()d
1026 * it when we weren't looking. One reference is
1027 * owned by the descriptor array, the other by us.
1029 if (fdp->fd_ofiles[indx] == fp) {
1030 fdp->fd_ofiles[indx] = NULL;
1036 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY, td);
1037 fp->f_flag |= FHASLOCK;
1039 /* assert that vn_open created a backing object if one is needed */
1040 KASSERT(!vn_canvmio(vp) || VOP_GETVOBJECT(vp, NULL) == 0,
1041 ("open: vmio vnode has no backing object after vn_open"));
1042 VOP_UNLOCK(vp, 0, td);
1045 * release our private reference, leaving the one associated with the
1046 * descriptor table intact.
1049 uap->sysmsg_result = indx;
1055 * ocreat(char *path, int mode)
1060 ocreat(struct ocreat_args *uap)
1062 struct open_args /* {
1063 syscallarg(char *) path;
1064 syscallarg(int) flags;
1065 syscallarg(int) mode;
1068 SCARG(&nuap, path) = SCARG(uap, path);
1069 SCARG(&nuap, mode) = SCARG(uap, mode);
1070 SCARG(&nuap, flags) = O_WRONLY | O_CREAT | O_TRUNC;
1071 return (open(&nuap));
1073 #endif /* COMPAT_43 */
1076 * mknod_args(char *path, int mode, int dev)
1078 * Create a special file.
1082 mknod(struct mknod_args *uap)
1084 struct thread *td = curthread;
1085 struct proc *p = td->td_proc;
1090 struct nameidata nd;
1094 switch (SCARG(uap, mode) & S_IFMT) {
1100 error = suser_cred(p->p_ucred, PRISON_ROOT);
1106 NDINIT(&nd, NAMEI_CREATE, CNP_LOCKPARENT, UIO_USERSPACE,
1107 SCARG(uap, path), td);
1108 if ((error = namei(&nd)) != 0)
1115 vattr.va_mode = (SCARG(uap, mode) & ALLPERMS) &~ p->p_fd->fd_cmask;
1116 vattr.va_rdev = SCARG(uap, dev);
1119 switch (SCARG(uap, mode) & S_IFMT) {
1120 case S_IFMT: /* used by badsect to flag bad sectors */
1121 vattr.va_type = VBAD;
1124 vattr.va_type = VCHR;
1127 vattr.va_type = VBLK;
1138 VOP_LEASE(nd.ni_dvp, td, p->p_ucred, LEASE_WRITE);
1140 error = VOP_WHITEOUT(nd.ni_dvp, NCPNULL, &nd.ni_cnd, NAMEI_CREATE);
1142 error = VOP_MKNOD(nd.ni_dvp, NCPNULL, &nd.ni_vp,
1143 &nd.ni_cnd, &vattr);
1147 NDFREE(&nd, NDF_ONLY_PNBUF);
1150 NDFREE(&nd, NDF_ONLY_PNBUF);
1151 if (nd.ni_dvp == vp)
1158 ASSERT_VOP_UNLOCKED(nd.ni_dvp, "mknod");
1159 ASSERT_VOP_UNLOCKED(nd.ni_vp, "mknod");
1164 * mkfifo_args(char *path, int mode)
1166 * Create a named pipe.
1170 mkfifo(struct mkfifo_args *uap)
1172 struct thread *td = curthread;
1173 struct proc *p = td->td_proc;
1176 struct nameidata nd;
1179 NDINIT(&nd, NAMEI_CREATE, CNP_LOCKPARENT, UIO_USERSPACE,
1180 SCARG(uap, path), td);
1181 if ((error = namei(&nd)) != 0)
1183 if (nd.ni_vp != NULL) {
1184 NDFREE(&nd, NDF_ONLY_PNBUF);
1185 if (nd.ni_dvp == nd.ni_vp)
1193 vattr.va_type = VFIFO;
1194 vattr.va_mode = (SCARG(uap, mode) & ALLPERMS) &~ p->p_fd->fd_cmask;
1195 VOP_LEASE(nd.ni_dvp, td, p->p_ucred, LEASE_WRITE);
1196 error = VOP_MKNOD(nd.ni_dvp, NCPNULL, &nd.ni_vp, &nd.ni_cnd, &vattr);
1199 NDFREE(&nd, NDF_ONLY_PNBUF);
1205 * link_args(char *path, char *link)
1207 * Make a hard file link.
1211 link(struct link_args *uap)
1213 struct thread *td = curthread;
1214 struct proc *p = td->td_proc;
1216 struct nameidata nd;
1220 NDINIT(&nd, NAMEI_LOOKUP, CNP_FOLLOW | CNP_NOOBJ, UIO_USERSPACE,
1221 SCARG(uap, path), td);
1222 if ((error = namei(&nd)) != 0)
1224 NDFREE(&nd, NDF_ONLY_PNBUF);
1226 if (vp->v_type == VDIR)
1227 error = EPERM; /* POSIX */
1229 NDINIT(&nd, NAMEI_CREATE, CNP_LOCKPARENT | CNP_NOOBJ,
1230 UIO_USERSPACE, SCARG(uap, link), td);
1233 if (nd.ni_vp != NULL) {
1238 VOP_LEASE(nd.ni_dvp, td, p->p_ucred,
1240 VOP_LEASE(vp, td, p->p_ucred, LEASE_WRITE);
1241 error = VOP_LINK(nd.ni_dvp, NCPNULL, vp, &nd.ni_cnd);
1243 NDFREE(&nd, NDF_ONLY_PNBUF);
1244 if (nd.ni_dvp == nd.ni_vp)
1251 ASSERT_VOP_UNLOCKED(nd.ni_dvp, "link");
1252 ASSERT_VOP_UNLOCKED(nd.ni_vp, "link");
1257 * symlink(char *path, char *link)
1259 * Make a symbolic link.
1263 symlink(struct symlink_args *uap)
1265 struct thread *td = curthread;
1266 struct proc *p = td->td_proc;
1270 struct nameidata nd;
1272 path = zalloc(namei_zone);
1273 if ((error = copyinstr(SCARG(uap, path), path, MAXPATHLEN, NULL)) != 0)
1276 NDINIT(&nd, NAMEI_CREATE, CNP_LOCKPARENT | CNP_NOOBJ, UIO_USERSPACE,
1277 SCARG(uap, link), td);
1278 if ((error = namei(&nd)) != 0)
1281 NDFREE(&nd, NDF_ONLY_PNBUF);
1282 if (nd.ni_dvp == nd.ni_vp)
1291 vattr.va_mode = ACCESSPERMS &~ p->p_fd->fd_cmask;
1292 VOP_LEASE(nd.ni_dvp, td, p->p_ucred, LEASE_WRITE);
1293 error = VOP_SYMLINK(nd.ni_dvp, NCPNULL, &nd.ni_vp, &nd.ni_cnd, &vattr, path);
1294 NDFREE(&nd, NDF_ONLY_PNBUF);
1298 ASSERT_VOP_UNLOCKED(nd.ni_dvp, "symlink");
1299 ASSERT_VOP_UNLOCKED(nd.ni_vp, "symlink");
1301 zfree(namei_zone, path);
1306 * undelete_args(char *path)
1308 * Delete a whiteout from the filesystem.
1312 undelete(struct undelete_args *uap)
1314 struct thread *td = curthread;
1315 struct proc *p = td->td_proc;
1317 struct nameidata nd;
1320 NDINIT(&nd, NAMEI_DELETE, CNP_LOCKPARENT | CNP_DOWHITEOUT, UIO_USERSPACE,
1321 SCARG(uap, path), td);
1326 if (nd.ni_vp != NULLVP || !(nd.ni_cnd.cn_flags & CNP_ISWHITEOUT)) {
1327 NDFREE(&nd, NDF_ONLY_PNBUF);
1328 if (nd.ni_dvp == nd.ni_vp)
1337 VOP_LEASE(nd.ni_dvp, td, p->p_ucred, LEASE_WRITE);
1338 error = VOP_WHITEOUT(nd.ni_dvp, NCPNULL, &nd.ni_cnd, NAMEI_DELETE);
1339 NDFREE(&nd, NDF_ONLY_PNBUF);
1341 ASSERT_VOP_UNLOCKED(nd.ni_dvp, "undelete");
1342 ASSERT_VOP_UNLOCKED(nd.ni_vp, "undelete");
1347 * unlink_args(char *path)
1349 * Delete a name from the filesystem.
1352 unlink(struct unlink_args *uap)
1354 struct thread *td = curthread;
1355 struct proc *p = td->td_proc;
1358 struct nameidata nd;
1361 NDINIT(&nd, NAMEI_DELETE, CNP_LOCKPARENT, UIO_USERSPACE, SCARG(uap, path), td);
1362 if ((error = namei(&nd)) != 0)
1365 VOP_LEASE(vp, td, p->p_ucred, LEASE_WRITE);
1366 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY, td);
1368 if (vp->v_type == VDIR)
1369 error = EPERM; /* POSIX */
1372 * The root of a mounted filesystem cannot be deleted.
1374 * XXX: can this only be a VDIR case?
1376 if (vp->v_flag & VROOT)
1381 VOP_LEASE(nd.ni_dvp, td, p->p_ucred, LEASE_WRITE);
1382 error = VOP_REMOVE(nd.ni_dvp, NCPNULL, vp, &nd.ni_cnd);
1384 NDFREE(&nd, NDF_ONLY_PNBUF);
1385 if (nd.ni_dvp == vp)
1391 ASSERT_VOP_UNLOCKED(nd.ni_dvp, "unlink");
1392 ASSERT_VOP_UNLOCKED(nd.ni_vp, "unlink");
1397 * lseek_args(int fd, int pad, off_t offset, int whence)
1399 * Reposition read/write file offset.
1402 lseek(struct lseek_args *uap)
1404 struct thread *td = curthread;
1405 struct proc *p = td->td_proc;
1406 struct filedesc *fdp = p->p_fd;
1411 if ((u_int)SCARG(uap, fd) >= fdp->fd_nfiles ||
1412 (fp = fdp->fd_ofiles[SCARG(uap, fd)]) == NULL)
1414 if (fp->f_type != DTYPE_VNODE)
1416 switch (SCARG(uap, whence)) {
1418 fp->f_offset += SCARG(uap, offset);
1421 error=VOP_GETATTR((struct vnode *)fp->f_data, &vattr, td);
1424 fp->f_offset = SCARG(uap, offset) + vattr.va_size;
1427 fp->f_offset = SCARG(uap, offset);
1432 uap->sysmsg_offset = fp->f_offset;
1436 #if defined(COMPAT_43) || defined(COMPAT_SUNOS)
1438 * Reposition read/write file offset.
1440 * olseek_args(int fd, long offset, int whence)
1443 olseek(struct olseek_args *uap)
1445 struct lseek_args /* {
1447 syscallarg(int) pad;
1448 syscallarg(off_t) offset;
1449 syscallarg(int) whence;
1453 SCARG(&nuap, fd) = SCARG(uap, fd);
1454 SCARG(&nuap, offset) = SCARG(uap, offset);
1455 SCARG(&nuap, whence) = SCARG(uap, whence);
1456 error = lseek(&nuap);
1459 #endif /* COMPAT_43 */
1462 * access_args(char *path, int flags)
1464 * Check access permissions.
1467 access(struct access_args *uap)
1469 struct thread *td = curthread;
1470 struct proc *p = td->td_proc;
1471 struct ucred *cred, *tmpcred;
1474 struct nameidata nd;
1478 * Create and modify a temporary credential instead of one that
1479 * is potentially shared. This could also mess up socket
1480 * buffer accounting which can run in an interrupt context.
1482 tmpcred = crdup(cred);
1483 tmpcred->cr_uid = p->p_ucred->cr_ruid;
1484 tmpcred->cr_groups[0] = p->p_ucred->cr_rgid;
1485 p->p_ucred = tmpcred;
1486 NDINIT(&nd, NAMEI_LOOKUP, CNP_FOLLOW | CNP_LOCKLEAF | CNP_NOOBJ,
1487 UIO_USERSPACE, SCARG(uap, path), td);
1488 if ((error = namei(&nd)) != 0)
1492 /* Flags == 0 means only check for existence. */
1493 if (SCARG(uap, flags)) {
1495 if (SCARG(uap, flags) & R_OK)
1497 if (SCARG(uap, flags) & W_OK)
1499 if (SCARG(uap, flags) & X_OK)
1501 if ((flags & VWRITE) == 0 || (error = vn_writechk(vp)) == 0)
1502 error = VOP_ACCESS(vp, flags, tmpcred, td);
1504 NDFREE(&nd, NDF_ONLY_PNBUF);
1512 #if defined(COMPAT_43) || defined(COMPAT_SUNOS)
1514 * ostat_args(char *path, struct ostat *ub)
1516 * Get file status; this version follows links.
1520 ostat(struct ostat_args *uap)
1522 struct thread *td = curthread;
1526 struct nameidata nd;
1528 NDINIT(&nd, NAMEI_LOOKUP, CNP_FOLLOW | CNP_LOCKLEAF | CNP_NOOBJ,
1529 UIO_USERSPACE, SCARG(uap, path), td);
1530 if ((error = namei(&nd)) != 0)
1532 NDFREE(&nd, NDF_ONLY_PNBUF);
1533 error = vn_stat(nd.ni_vp, &sb, td);
1538 error = copyout((caddr_t)&osb, (caddr_t)SCARG(uap, ub), sizeof (osb));
1543 * olstat_args(char *path, struct ostat *ub)
1545 * Get file status; this version does not follow links.
1549 olstat(struct olstat_args *uap)
1551 struct thread *td = curthread;
1556 struct nameidata nd;
1558 NDINIT(&nd, NAMEI_LOOKUP, CNP_LOCKLEAF | CNP_NOOBJ,
1559 UIO_USERSPACE, SCARG(uap, path), td);
1560 if ((error = namei(&nd)) != 0)
1563 error = vn_stat(vp, &sb, td);
1564 NDFREE(&nd, NDF_ONLY_PNBUF);
1569 error = copyout((caddr_t)&osb, (caddr_t)SCARG(uap, ub), sizeof (osb));
1574 * Convert from an old to a new stat structure.
1581 ost->st_dev = st->st_dev;
1582 ost->st_ino = st->st_ino;
1583 ost->st_mode = st->st_mode;
1584 ost->st_nlink = st->st_nlink;
1585 ost->st_uid = st->st_uid;
1586 ost->st_gid = st->st_gid;
1587 ost->st_rdev = st->st_rdev;
1588 if (st->st_size < (quad_t)1 << 32)
1589 ost->st_size = st->st_size;
1592 ost->st_atime = st->st_atime;
1593 ost->st_mtime = st->st_mtime;
1594 ost->st_ctime = st->st_ctime;
1595 ost->st_blksize = st->st_blksize;
1596 ost->st_blocks = st->st_blocks;
1597 ost->st_flags = st->st_flags;
1598 ost->st_gen = st->st_gen;
1600 #endif /* COMPAT_43 || COMPAT_SUNOS */
1603 * stat_args(char *path, struct stat *ub)
1605 * Get file status; this version follows links.
1609 stat(struct stat_args *uap)
1611 struct thread *td = curthread;
1614 struct nameidata nd;
1616 NDINIT(&nd, NAMEI_LOOKUP, CNP_FOLLOW | CNP_LOCKLEAF | CNP_NOOBJ,
1617 UIO_USERSPACE, SCARG(uap, path), td);
1618 if ((error = namei(&nd)) != 0)
1620 error = vn_stat(nd.ni_vp, &sb, td);
1621 NDFREE(&nd, NDF_ONLY_PNBUF);
1625 error = copyout((caddr_t)&sb, (caddr_t)SCARG(uap, ub), sizeof (sb));
1630 * lstat_args(char *path, struct stat *ub)
1632 * Get file status; this version does not follow links.
1636 lstat(struct lstat_args *uap)
1638 struct thread *td = curthread;
1642 struct nameidata nd;
1644 NDINIT(&nd, NAMEI_LOOKUP, CNP_LOCKLEAF | CNP_NOOBJ,
1645 UIO_USERSPACE, SCARG(uap, path), td);
1646 if ((error = namei(&nd)) != 0)
1649 error = vn_stat(vp, &sb, td);
1650 NDFREE(&nd, NDF_ONLY_PNBUF);
1654 error = copyout((caddr_t)&sb, (caddr_t)SCARG(uap, ub), sizeof (sb));
1663 nsb->st_dev = sb->st_dev;
1664 nsb->st_ino = sb->st_ino;
1665 nsb->st_mode = sb->st_mode;
1666 nsb->st_nlink = sb->st_nlink;
1667 nsb->st_uid = sb->st_uid;
1668 nsb->st_gid = sb->st_gid;
1669 nsb->st_rdev = sb->st_rdev;
1670 nsb->st_atimespec = sb->st_atimespec;
1671 nsb->st_mtimespec = sb->st_mtimespec;
1672 nsb->st_ctimespec = sb->st_ctimespec;
1673 nsb->st_size = sb->st_size;
1674 nsb->st_blocks = sb->st_blocks;
1675 nsb->st_blksize = sb->st_blksize;
1676 nsb->st_flags = sb->st_flags;
1677 nsb->st_gen = sb->st_gen;
1678 nsb->st_qspare[0] = sb->st_qspare[0];
1679 nsb->st_qspare[1] = sb->st_qspare[1];
1683 * nstat_args(char *path, struct nstat *ub)
1687 nstat(struct nstat_args *uap)
1689 struct thread *td = curthread;
1693 struct nameidata nd;
1695 NDINIT(&nd, NAMEI_LOOKUP, CNP_FOLLOW | CNP_LOCKLEAF | CNP_NOOBJ,
1696 UIO_USERSPACE, SCARG(uap, path), td);
1697 if ((error = namei(&nd)) != 0)
1699 NDFREE(&nd, NDF_ONLY_PNBUF);
1700 error = vn_stat(nd.ni_vp, &sb, td);
1704 cvtnstat(&sb, &nsb);
1705 error = copyout((caddr_t)&nsb, (caddr_t)SCARG(uap, ub), sizeof (nsb));
1710 * lstat_args(char *path, struct stat *ub)
1712 * Get file status; this version does not follow links.
1716 nlstat(struct nlstat_args *uap)
1718 struct thread *td = curthread;
1723 struct nameidata nd;
1725 NDINIT(&nd, NAMEI_LOOKUP, CNP_LOCKLEAF | CNP_NOOBJ,
1726 UIO_USERSPACE, SCARG(uap, path), td);
1727 if ((error = namei(&nd)) != 0)
1730 NDFREE(&nd, NDF_ONLY_PNBUF);
1731 error = vn_stat(vp, &sb, td);
1735 cvtnstat(&sb, &nsb);
1736 error = copyout((caddr_t)&nsb, (caddr_t)SCARG(uap, ub), sizeof (nsb));
1741 * pathconf_Args(char *path, int name)
1743 * Get configurable pathname variables.
1747 pathconf(struct pathconf_args *uap)
1749 struct thread *td = curthread;
1751 struct nameidata nd;
1753 NDINIT(&nd, NAMEI_LOOKUP, CNP_FOLLOW | CNP_LOCKLEAF | CNP_NOOBJ,
1754 UIO_USERSPACE, SCARG(uap, path), td);
1755 if ((error = namei(&nd)) != 0)
1757 NDFREE(&nd, NDF_ONLY_PNBUF);
1758 error = VOP_PATHCONF(nd.ni_vp, SCARG(uap, name), uap->sysmsg_fds);
1764 * readlink_args(char *path, char *buf, int count)
1766 * Return target name of a symbolic link.
1770 readlink(struct readlink_args *uap)
1772 struct thread *td = curthread;
1773 struct proc *p = td->td_proc;
1778 struct nameidata nd;
1780 NDINIT(&nd, NAMEI_LOOKUP, CNP_LOCKLEAF | CNP_NOOBJ,
1781 UIO_USERSPACE, SCARG(uap, path), td);
1782 if ((error = namei(&nd)) != 0)
1784 NDFREE(&nd, NDF_ONLY_PNBUF);
1786 if (vp->v_type != VLNK)
1789 aiov.iov_base = SCARG(uap, buf);
1790 aiov.iov_len = SCARG(uap, count);
1791 auio.uio_iov = &aiov;
1792 auio.uio_iovcnt = 1;
1793 auio.uio_offset = 0;
1794 auio.uio_rw = UIO_READ;
1795 auio.uio_segflg = UIO_USERSPACE;
1797 auio.uio_resid = SCARG(uap, count);
1798 error = VOP_READLINK(vp, &auio, p->p_ucred);
1801 uap->sysmsg_result = SCARG(uap, count) - auio.uio_resid;
1806 setfflags(struct vnode *vp, int flags)
1808 struct thread *td = curthread;
1809 struct proc *p = td->td_proc;
1814 * Prevent non-root users from setting flags on devices. When
1815 * a device is reused, users can retain ownership of the device
1816 * if they are allowed to set flags and programs assume that
1817 * chown can't fail when done as root.
1819 if ((vp->v_type == VCHR || vp->v_type == VBLK) &&
1820 ((error = suser_cred(p->p_ucred, PRISON_ROOT)) != 0))
1823 VOP_LEASE(vp, td, p->p_ucred, LEASE_WRITE);
1824 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY, td);
1826 vattr.va_flags = flags;
1827 error = VOP_SETATTR(vp, &vattr, p->p_ucred, td);
1828 VOP_UNLOCK(vp, 0, td);
1833 * chflags(char *path, int flags)
1835 * Change flags of a file given a path name.
1839 chflags(struct chflags_args *uap)
1841 struct thread *td = curthread;
1843 struct nameidata nd;
1845 NDINIT(&nd, NAMEI_LOOKUP, CNP_FOLLOW, UIO_USERSPACE,
1846 SCARG(uap, path), td);
1847 if ((error = namei(&nd)) != 0)
1849 NDFREE(&nd, NDF_ONLY_PNBUF);
1850 error = setfflags(nd.ni_vp, SCARG(uap, flags));
1856 * fchflags_args(int fd, int flags)
1858 * Change flags of a file given a file descriptor.
1862 fchflags(struct fchflags_args *uap)
1864 struct thread *td = curthread;
1865 struct proc *p = td->td_proc;
1869 if ((error = getvnode(p->p_fd, SCARG(uap, fd), &fp)) != 0)
1871 return setfflags((struct vnode *) fp->f_data, SCARG(uap, flags));
1875 setfmode(struct vnode *vp, int mode)
1877 struct thread *td = curthread;
1878 struct proc *p = td->td_proc;
1882 VOP_LEASE(vp, td, p->p_ucred, LEASE_WRITE);
1883 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY, td);
1885 vattr.va_mode = mode & ALLPERMS;
1886 error = VOP_SETATTR(vp, &vattr, p->p_ucred, td);
1887 VOP_UNLOCK(vp, 0, td);
1892 * chmod_args(char *path, int mode)
1894 * Change mode of a file given path name.
1898 chmod(struct chmod_args *uap)
1900 struct thread *td = curthread;
1902 struct nameidata nd;
1904 NDINIT(&nd, NAMEI_LOOKUP, CNP_FOLLOW, UIO_USERSPACE,
1905 SCARG(uap, path), td);
1906 if ((error = namei(&nd)) != 0)
1908 NDFREE(&nd, NDF_ONLY_PNBUF);
1909 error = setfmode(nd.ni_vp, SCARG(uap, mode));
1915 * lchmod_args(char *path, int mode)
1917 * Change mode of a file given path name (don't follow links.)
1921 lchmod(struct lchmod_args *uap)
1923 struct thread *td = curthread;
1925 struct nameidata nd;
1927 NDINIT(&nd, NAMEI_LOOKUP, 0, UIO_USERSPACE, SCARG(uap, path), td);
1928 if ((error = namei(&nd)) != 0)
1930 NDFREE(&nd, NDF_ONLY_PNBUF);
1931 error = setfmode(nd.ni_vp, SCARG(uap, mode));
1937 * fchmod_args(int fd, int mode)
1939 * Change mode of a file given a file descriptor.
1943 fchmod(struct fchmod_args *uap)
1945 struct thread *td = curthread;
1946 struct proc *p = td->td_proc;
1950 if ((error = getvnode(p->p_fd, SCARG(uap, fd), &fp)) != 0)
1952 return setfmode((struct vnode *)fp->f_data, SCARG(uap, mode));
1956 setfown(struct vnode *vp, uid_t uid, gid_t gid)
1958 struct thread *td = curthread;
1959 struct proc *p = td->td_proc;
1963 VOP_LEASE(vp, td, p->p_ucred, LEASE_WRITE);
1964 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY, td);
1968 error = VOP_SETATTR(vp, &vattr, p->p_ucred, td);
1969 VOP_UNLOCK(vp, 0, td);
1974 * chown(char *path, int uid, int gid)
1976 * Set ownership given a path name.
1980 chown(struct chown_args *uap)
1982 struct thread *td = curthread;
1984 struct nameidata nd;
1986 NDINIT(&nd, NAMEI_LOOKUP, CNP_FOLLOW, UIO_USERSPACE,
1987 SCARG(uap, path), td);
1988 if ((error = namei(&nd)) != 0)
1990 NDFREE(&nd, NDF_ONLY_PNBUF);
1991 error = setfown(nd.ni_vp, SCARG(uap, uid), SCARG(uap, gid));
1997 * lchown_args(char *path, int uid, int gid)
1999 * Set ownership given a path name, do not cross symlinks.
2003 lchown(struct lchown_args *uap)
2005 struct thread *td = curthread;
2007 struct nameidata nd;
2009 NDINIT(&nd, NAMEI_LOOKUP, 0, UIO_USERSPACE, SCARG(uap, path), td);
2010 if ((error = namei(&nd)) != 0)
2012 NDFREE(&nd, NDF_ONLY_PNBUF);
2013 error = setfown(nd.ni_vp, SCARG(uap, uid), SCARG(uap, gid));
2019 * fchown_args(int fd, int uid, int gid)
2021 * Set ownership given a file descriptor.
2025 fchown(struct fchown_args *uap)
2027 struct thread *td = curthread;
2028 struct proc *p = td->td_proc;
2032 if ((error = getvnode(p->p_fd, SCARG(uap, fd), &fp)) != 0)
2034 return setfown((struct vnode *)fp->f_data,
2035 SCARG(uap, uid), SCARG(uap, gid));
2039 getutimes(const struct timeval *usrtvp, struct timespec *tsp)
2041 struct timeval tv[2];
2044 if (usrtvp == NULL) {
2046 TIMEVAL_TO_TIMESPEC(&tv[0], &tsp[0]);
2049 if ((error = copyin(usrtvp, tv, sizeof (tv))) != 0)
2051 TIMEVAL_TO_TIMESPEC(&tv[0], &tsp[0]);
2052 TIMEVAL_TO_TIMESPEC(&tv[1], &tsp[1]);
2058 setutimes(struct vnode *vp, const struct timespec *ts, int nullflag)
2060 struct thread *td = curthread;
2061 struct proc *p = td->td_proc;
2065 VOP_LEASE(vp, td, p->p_ucred, LEASE_WRITE);
2066 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY, td);
2068 vattr.va_atime = ts[0];
2069 vattr.va_mtime = ts[1];
2071 vattr.va_vaflags |= VA_UTIMES_NULL;
2072 error = VOP_SETATTR(vp, &vattr, p->p_ucred, td);
2073 VOP_UNLOCK(vp, 0, td);
2078 * utimes_args(char *path, struct timeval *tptr)
2080 * Set the access and modification times of a file.
2084 utimes(struct utimes_args *uap)
2086 struct thread *td = curthread;
2087 struct timespec ts[2];
2088 struct timeval *usrtvp;
2090 struct nameidata nd;
2092 usrtvp = SCARG(uap, tptr);
2093 if ((error = getutimes(usrtvp, ts)) != 0)
2095 NDINIT(&nd, NAMEI_LOOKUP, CNP_FOLLOW, UIO_USERSPACE, SCARG(uap, path), td);
2096 if ((error = namei(&nd)) != 0)
2098 NDFREE(&nd, NDF_ONLY_PNBUF);
2099 error = setutimes(nd.ni_vp, ts, usrtvp == NULL);
2105 * lutimes_args(char *path, struct timeval *tptr)
2107 * Set the access and modification times of a file.
2111 lutimes(struct lutimes_args *uap)
2113 struct thread *td = curthread;
2114 struct timespec ts[2];
2115 struct timeval *usrtvp;
2117 struct nameidata nd;
2119 usrtvp = SCARG(uap, tptr);
2120 if ((error = getutimes(usrtvp, ts)) != 0)
2122 NDINIT(&nd, NAMEI_LOOKUP, 0, UIO_USERSPACE, SCARG(uap, path), td);
2123 if ((error = namei(&nd)) != 0)
2125 NDFREE(&nd, NDF_ONLY_PNBUF);
2126 error = setutimes(nd.ni_vp, ts, usrtvp == NULL);
2132 * futimes_args(int fd, struct timeval *tptr)
2134 * Set the access and modification times of a file.
2138 futimes(struct futimes_args *uap)
2140 struct thread *td = curthread;
2141 struct proc *p = td->td_proc;
2142 struct timespec ts[2];
2144 struct timeval *usrtvp;
2147 usrtvp = SCARG(uap, tptr);
2148 if ((error = getutimes(usrtvp, ts)) != 0)
2150 if ((error = getvnode(p->p_fd, SCARG(uap, fd), &fp)) != 0)
2152 return setutimes((struct vnode *)fp->f_data, ts, usrtvp == NULL);
2156 * truncate(char *path, int pad, off_t length)
2158 * Truncate a file given its path name.
2162 truncate(struct truncate_args *uap)
2164 struct thread *td = curthread;
2165 struct proc *p = td->td_proc;
2169 struct nameidata nd;
2171 if (uap->length < 0)
2173 NDINIT(&nd, NAMEI_LOOKUP, CNP_FOLLOW, UIO_USERSPACE, SCARG(uap, path), td);
2174 if ((error = namei(&nd)) != 0)
2177 NDFREE(&nd, NDF_ONLY_PNBUF);
2178 VOP_LEASE(vp, td, p->p_ucred, LEASE_WRITE);
2179 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY, td);
2180 if (vp->v_type == VDIR)
2182 else if ((error = vn_writechk(vp)) == 0 &&
2183 (error = VOP_ACCESS(vp, VWRITE, p->p_ucred, td)) == 0) {
2185 vattr.va_size = SCARG(uap, length);
2186 error = VOP_SETATTR(vp, &vattr, p->p_ucred, td);
2193 kern_ftruncate(int fd, off_t length)
2195 struct thread *td = curthread;
2196 struct proc *p = td->td_proc;
2204 if ((error = getvnode(p->p_fd, fd, &fp)) != 0)
2206 if ((fp->f_flag & FWRITE) == 0)
2208 vp = (struct vnode *)fp->f_data;
2209 VOP_LEASE(vp, td, p->p_ucred, LEASE_WRITE);
2210 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY, td);
2211 if (vp->v_type == VDIR)
2213 else if ((error = vn_writechk(vp)) == 0) {
2215 vattr.va_size = length;
2216 error = VOP_SETATTR(vp, &vattr, fp->f_cred, td);
2218 VOP_UNLOCK(vp, 0, td);
2223 * ftruncate_args(int fd, int pad, off_t length)
2225 * Truncate a file given a file descriptor.
2229 ftruncate(struct ftruncate_args *uap)
2233 error = kern_ftruncate(uap->fd, uap->length);
2238 #if defined(COMPAT_43) || defined(COMPAT_SUNOS)
2240 * otruncate_args(char *path, long length)
2242 * Truncate a file given its path name.
2246 otruncate(struct otruncate_args *uap)
2248 struct truncate_args /* {
2249 syscallarg(char *) path;
2250 syscallarg(int) pad;
2251 syscallarg(off_t) length;
2254 SCARG(&nuap, path) = SCARG(uap, path);
2255 SCARG(&nuap, length) = SCARG(uap, length);
2256 return (truncate(&nuap));
2258 #endif /* COMPAT_43 || COMPAT_SUNOS */
2263 * Sync an open file.
2267 fsync(struct fsync_args *uap)
2269 struct thread *td = curthread;
2270 struct proc *p = td->td_proc;
2276 if ((error = getvnode(p->p_fd, SCARG(uap, fd), &fp)) != 0)
2278 vp = (struct vnode *)fp->f_data;
2279 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY, td);
2280 if (VOP_GETVOBJECT(vp, &obj) == 0)
2281 vm_object_page_clean(obj, 0, 0, 0);
2282 if ((error = VOP_FSYNC(vp, MNT_WAIT, td)) == 0 &&
2283 vp->v_mount && (vp->v_mount->mnt_flag & MNT_SOFTDEP) &&
2285 error = (*bioops.io_fsync)(vp);
2286 VOP_UNLOCK(vp, 0, td);
2291 * rename_args(char *from, char *to)
2293 * Rename files. Source and destination must either both be directories,
2294 * or both not be directories. If target is a directory, it must be empty.
2298 rename(struct rename_args *uap)
2300 struct thread *td = curthread;
2301 struct proc *p = td->td_proc;
2302 struct vnode *tvp, *fvp, *tdvp;
2303 struct nameidata fromnd, tond;
2307 NDINIT(&fromnd, NAMEI_DELETE, CNP_WANTPARENT | CNP_SAVESTART,
2308 UIO_USERSPACE, SCARG(uap, from), td);
2309 if ((error = namei(&fromnd)) != 0)
2312 NDINIT(&tond, NAMEI_RENAME,
2313 CNP_LOCKPARENT | CNP_LOCKLEAF | CNP_NOCACHE |
2314 CNP_SAVESTART | CNP_NOOBJ,
2315 UIO_USERSPACE, SCARG(uap, to), td);
2316 if (fromnd.ni_vp->v_type == VDIR)
2317 tond.ni_cnd.cn_flags |= CNP_WILLBEDIR;
2318 if ((error = namei(&tond)) != 0) {
2319 /* Translate error code for rename("dir1", "dir2/."). */
2320 if (error == EISDIR && fvp->v_type == VDIR)
2322 NDFREE(&fromnd, NDF_ONLY_PNBUF);
2323 vrele(fromnd.ni_dvp);
2330 if (fvp->v_type == VDIR && tvp->v_type != VDIR) {
2333 } else if (fvp->v_type != VDIR && tvp->v_type == VDIR) {
2341 * If the source is the same as the destination (that is, if they
2342 * are links to the same vnode), then there is nothing to do.
2348 VOP_LEASE(tdvp, td, p->p_ucred, LEASE_WRITE);
2349 if (fromnd.ni_dvp != tdvp) {
2350 VOP_LEASE(fromnd.ni_dvp, td, p->p_ucred, LEASE_WRITE);
2353 VOP_LEASE(tvp, td, p->p_ucred, LEASE_WRITE);
2355 error = VOP_RENAME(fromnd.ni_dvp, NCPNULL, fromnd.ni_vp, &fromnd.ni_cnd,
2356 tond.ni_dvp, NCPNULL, tond.ni_vp, &tond.ni_cnd);
2357 NDFREE(&fromnd, NDF_ONLY_PNBUF);
2358 NDFREE(&tond, NDF_ONLY_PNBUF);
2360 NDFREE(&fromnd, NDF_ONLY_PNBUF);
2361 NDFREE(&tond, NDF_ONLY_PNBUF);
2368 vrele(fromnd.ni_dvp);
2371 vrele(tond.ni_startdir);
2372 ASSERT_VOP_UNLOCKED(fromnd.ni_dvp, "rename");
2373 ASSERT_VOP_UNLOCKED(fromnd.ni_vp, "rename");
2374 ASSERT_VOP_UNLOCKED(tond.ni_dvp, "rename");
2375 ASSERT_VOP_UNLOCKED(tond.ni_vp, "rename");
2377 if (fromnd.ni_startdir)
2378 vrele(fromnd.ni_startdir);
2385 * mkdir_args(char *path, int mode)
2387 * Make a directory file.
2391 mkdir(struct mkdir_args *uap)
2393 struct thread *td = curthread;
2394 struct proc *p = td->td_proc;
2398 struct nameidata nd;
2401 NDINIT(&nd, NAMEI_CREATE, CNP_LOCKPARENT, UIO_USERSPACE,
2402 SCARG(uap, path), td);
2403 nd.ni_cnd.cn_flags |= CNP_WILLBEDIR;
2404 if ((error = namei(&nd)) != 0)
2408 NDFREE(&nd, NDF_ONLY_PNBUF);
2409 if (nd.ni_dvp == vp)
2417 vattr.va_type = VDIR;
2418 vattr.va_mode = (SCARG(uap, mode) & ACCESSPERMS) &~ p->p_fd->fd_cmask;
2419 VOP_LEASE(nd.ni_dvp, td, p->p_ucred, LEASE_WRITE);
2420 error = VOP_MKDIR(nd.ni_dvp, NCPNULL, &nd.ni_vp, &nd.ni_cnd, &vattr);
2421 NDFREE(&nd, NDF_ONLY_PNBUF);
2425 ASSERT_VOP_UNLOCKED(nd.ni_dvp, "mkdir");
2426 ASSERT_VOP_UNLOCKED(nd.ni_vp, "mkdir");
2431 * rmdir_args(char *path)
2433 * Remove a directory file.
2437 rmdir(struct rmdir_args *uap)
2439 struct thread *td = curthread;
2440 struct proc *p = td->td_proc;
2443 struct nameidata nd;
2446 NDINIT(&nd, NAMEI_DELETE, CNP_LOCKPARENT | CNP_LOCKLEAF, UIO_USERSPACE,
2447 SCARG(uap, path), td);
2448 if ((error = namei(&nd)) != 0)
2451 if (vp->v_type != VDIR) {
2456 * No rmdir "." please.
2458 if (nd.ni_dvp == vp) {
2463 * The root of a mounted filesystem cannot be deleted.
2465 if (vp->v_flag & VROOT)
2468 VOP_LEASE(nd.ni_dvp, td, p->p_ucred, LEASE_WRITE);
2469 VOP_LEASE(vp, td, p->p_ucred, LEASE_WRITE);
2470 error = VOP_RMDIR(nd.ni_dvp, NCPNULL, nd.ni_vp, &nd.ni_cnd);
2473 NDFREE(&nd, NDF_ONLY_PNBUF);
2474 if (nd.ni_dvp == vp)
2480 ASSERT_VOP_UNLOCKED(nd.ni_dvp, "rmdir");
2481 ASSERT_VOP_UNLOCKED(nd.ni_vp, "rmdir");
2487 * ogetdirentries_args(int fd, char *buf, u_int count, long *basep)
2489 * Read a block of directory entries in a file system independent format.
2492 ogetdirentries(struct ogetdirentries_args *uap)
2494 struct thread *td = curthread;
2495 struct proc *p = td->td_proc;
2498 struct uio auio, kuio;
2499 struct iovec aiov, kiov;
2500 struct dirent *dp, *edp;
2502 int error, eofflag, readcnt;
2505 /* XXX arbitrary sanity limit on `count'. */
2506 if (SCARG(uap, count) > 64 * 1024)
2508 if ((error = getvnode(p->p_fd, SCARG(uap, fd), &fp)) != 0)
2510 if ((fp->f_flag & FREAD) == 0)
2512 vp = (struct vnode *)fp->f_data;
2514 if (vp->v_type != VDIR)
2516 aiov.iov_base = SCARG(uap, buf);
2517 aiov.iov_len = SCARG(uap, count);
2518 auio.uio_iov = &aiov;
2519 auio.uio_iovcnt = 1;
2520 auio.uio_rw = UIO_READ;
2521 auio.uio_segflg = UIO_USERSPACE;
2523 auio.uio_resid = SCARG(uap, count);
2524 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY, td);
2525 loff = auio.uio_offset = fp->f_offset;
2526 # if (BYTE_ORDER != LITTLE_ENDIAN)
2527 if (vp->v_mount->mnt_maxsymlinklen <= 0) {
2528 error = VOP_READDIR(vp, &auio, fp->f_cred, &eofflag,
2530 fp->f_offset = auio.uio_offset;
2535 kuio.uio_iov = &kiov;
2536 kuio.uio_segflg = UIO_SYSSPACE;
2537 kiov.iov_len = SCARG(uap, count);
2538 MALLOC(dirbuf, caddr_t, SCARG(uap, count), M_TEMP, M_WAITOK);
2539 kiov.iov_base = dirbuf;
2540 error = VOP_READDIR(vp, &kuio, fp->f_cred, &eofflag,
2542 fp->f_offset = kuio.uio_offset;
2544 readcnt = SCARG(uap, count) - kuio.uio_resid;
2545 edp = (struct dirent *)&dirbuf[readcnt];
2546 for (dp = (struct dirent *)dirbuf; dp < edp; ) {
2547 # if (BYTE_ORDER == LITTLE_ENDIAN)
2549 * The expected low byte of
2550 * dp->d_namlen is our dp->d_type.
2551 * The high MBZ byte of dp->d_namlen
2552 * is our dp->d_namlen.
2554 dp->d_type = dp->d_namlen;
2558 * The dp->d_type is the high byte
2559 * of the expected dp->d_namlen,
2560 * so must be zero'ed.
2564 if (dp->d_reclen > 0) {
2565 dp = (struct dirent *)
2566 ((char *)dp + dp->d_reclen);
2573 error = uiomove(dirbuf, readcnt, &auio);
2575 FREE(dirbuf, M_TEMP);
2577 VOP_UNLOCK(vp, 0, td);
2580 if (SCARG(uap, count) == auio.uio_resid) {
2581 if (union_dircheckp) {
2582 error = union_dircheckp(td, &vp, fp);
2588 if ((vp->v_flag & VROOT) &&
2589 (vp->v_mount->mnt_flag & MNT_UNION)) {
2590 struct vnode *tvp = vp;
2591 vp = vp->v_mount->mnt_vnodecovered;
2593 fp->f_data = (caddr_t) vp;
2599 error = copyout((caddr_t)&loff, (caddr_t)SCARG(uap, basep),
2601 uap->sysmsg_result = SCARG(uap, count) - auio.uio_resid;
2604 #endif /* COMPAT_43 */
2607 * getdirentries_args(int fd, char *buf, u_int conut, long *basep)
2609 * Read a block of directory entries in a file system independent format.
2612 getdirentries(struct getdirentries_args *uap)
2614 struct thread *td = curthread;
2615 struct proc *p = td->td_proc;
2623 if ((error = getvnode(p->p_fd, SCARG(uap, fd), &fp)) != 0)
2625 if ((fp->f_flag & FREAD) == 0)
2627 vp = (struct vnode *)fp->f_data;
2629 if (vp->v_type != VDIR)
2631 aiov.iov_base = SCARG(uap, buf);
2632 aiov.iov_len = SCARG(uap, count);
2633 auio.uio_iov = &aiov;
2634 auio.uio_iovcnt = 1;
2635 auio.uio_rw = UIO_READ;
2636 auio.uio_segflg = UIO_USERSPACE;
2638 auio.uio_resid = SCARG(uap, count);
2639 /* vn_lock(vp, LK_SHARED | LK_RETRY, td); */
2640 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY, td);
2641 loff = auio.uio_offset = fp->f_offset;
2642 error = VOP_READDIR(vp, &auio, fp->f_cred, &eofflag, NULL, NULL);
2643 fp->f_offset = auio.uio_offset;
2644 VOP_UNLOCK(vp, 0, td);
2647 if (SCARG(uap, count) == auio.uio_resid) {
2648 if (union_dircheckp) {
2649 error = union_dircheckp(td, &vp, fp);
2655 if ((vp->v_flag & VROOT) &&
2656 (vp->v_mount->mnt_flag & MNT_UNION)) {
2657 struct vnode *tvp = vp;
2658 vp = vp->v_mount->mnt_vnodecovered;
2660 fp->f_data = (caddr_t) vp;
2666 if (SCARG(uap, basep) != NULL) {
2667 error = copyout((caddr_t)&loff, (caddr_t)SCARG(uap, basep),
2670 uap->sysmsg_result = SCARG(uap, count) - auio.uio_resid;
2675 * getdents_args(int fd, char *buf, size_t count)
2678 getdents(struct getdents_args *uap)
2680 struct getdirentries_args ap;
2684 ap.count = uap->count;
2686 return getdirentries(&ap);
2690 * umask(int newmask)
2692 * Set the mode mask for creation of filesystem nodes.
2697 umask(struct umask_args *uap)
2699 struct thread *td = curthread;
2700 struct proc *p = td->td_proc;
2701 struct filedesc *fdp;
2704 uap->sysmsg_result = fdp->fd_cmask;
2705 fdp->fd_cmask = SCARG(uap, newmask) & ALLPERMS;
2710 * revoke(char *path)
2712 * Void all references to file by ripping underlying filesystem
2717 revoke(struct revoke_args *uap)
2719 struct thread *td = curthread;
2720 struct proc *p = td->td_proc;
2724 struct nameidata nd;
2726 NDINIT(&nd, NAMEI_LOOKUP, CNP_FOLLOW, UIO_USERSPACE, SCARG(uap, path), td);
2727 if ((error = namei(&nd)) != 0)
2730 NDFREE(&nd, NDF_ONLY_PNBUF);
2731 if (vp->v_type != VCHR && vp->v_type != VBLK) {
2735 if ((error = VOP_GETATTR(vp, &vattr, td)) != 0)
2737 if (p->p_ucred->cr_uid != vattr.va_uid &&
2738 (error = suser_cred(p->p_ucred, PRISON_ROOT)))
2741 VOP_REVOKE(vp, REVOKEALL);
2748 * Convert a user file descriptor to a kernel file entry.
2751 getvnode(struct filedesc *fdp, int fd, struct file **fpp)
2755 if ((u_int)fd >= fdp->fd_nfiles ||
2756 (fp = fdp->fd_ofiles[fd]) == NULL)
2758 if (fp->f_type != DTYPE_VNODE && fp->f_type != DTYPE_FIFO)
2764 * getfh_args(char *fname, fhandle_t *fhp)
2766 * Get (NFS) file handle
2769 getfh(struct getfh_args *uap)
2771 struct thread *td = curthread;
2772 struct nameidata nd;
2778 * Must be super user
2783 NDINIT(&nd, NAMEI_LOOKUP, CNP_FOLLOW | CNP_LOCKLEAF, UIO_USERSPACE, uap->fname, td);
2787 NDFREE(&nd, NDF_ONLY_PNBUF);
2789 bzero(&fh, sizeof(fh));
2790 fh.fh_fsid = vp->v_mount->mnt_stat.f_fsid;
2791 error = VFS_VPTOFH(vp, &fh.fh_fid);
2795 error = copyout(&fh, uap->fhp, sizeof (fh));
2800 * fhopen_args(const struct fhandle *u_fhp, int flags)
2802 * syscall for the rpc.lockd to use to translate a NFS file handle into
2803 * an open descriptor.
2805 * warning: do not remove the suser() call or this becomes one giant
2809 fhopen(struct fhopen_args *uap)
2811 struct thread *td = curthread;
2812 struct proc *p = td->td_proc;
2817 struct vattr *vap = &vat;
2820 struct filedesc *fdp = p->p_fd;
2821 int fmode, mode, error, type;
2826 * Must be super user
2832 fmode = FFLAGS(SCARG(uap, flags));
2833 /* why not allow a non-read/write open for our lockd? */
2834 if (((fmode & (FREAD | FWRITE)) == 0) || (fmode & O_CREAT))
2836 error = copyin(SCARG(uap,u_fhp), &fhp, sizeof(fhp));
2839 /* find the mount point */
2840 mp = vfs_getvfs(&fhp.fh_fsid);
2843 /* now give me my vnode, it gets returned to me locked */
2844 error = VFS_FHTOVP(mp, &fhp.fh_fid, &vp);
2848 * from now on we have to make sure not
2849 * to forget about the vnode
2850 * any error that causes an abort must vput(vp)
2851 * just set error = err and 'goto bad;'.
2857 if (vp->v_type == VLNK) {
2861 if (vp->v_type == VSOCK) {
2866 if (fmode & (FWRITE | O_TRUNC)) {
2867 if (vp->v_type == VDIR) {
2871 error = vn_writechk(vp);
2879 error = VOP_ACCESS(vp, mode, p->p_ucred, td);
2883 if (fmode & O_TRUNC) {
2884 VOP_UNLOCK(vp, 0, td); /* XXX */
2885 VOP_LEASE(vp, td, p->p_ucred, LEASE_WRITE);
2886 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY, td); /* XXX */
2889 error = VOP_SETATTR(vp, vap, p->p_ucred, td);
2893 error = VOP_OPEN(vp, fmode, p->p_ucred, td);
2897 * Make sure that a VM object is created for VMIO support.
2899 if (vn_canvmio(vp) == TRUE) {
2900 if ((error = vfs_object_create(vp, td)) != 0)
2907 * end of vn_open code
2910 if ((error = falloc(p, &nfp, &indx)) != 0) {
2918 * hold an extra reference to avoid having fp ripped out
2919 * from under us while we block in the lock op.
2922 nfp->f_data = (caddr_t)vp;
2923 nfp->f_flag = fmode & FMASK;
2924 nfp->f_ops = &vnops;
2925 nfp->f_type = DTYPE_VNODE;
2926 if (fmode & (O_EXLOCK | O_SHLOCK)) {
2927 lf.l_whence = SEEK_SET;
2930 if (fmode & O_EXLOCK)
2931 lf.l_type = F_WRLCK;
2933 lf.l_type = F_RDLCK;
2935 if ((fmode & FNONBLOCK) == 0)
2937 VOP_UNLOCK(vp, 0, td);
2938 if ((error = VOP_ADVLOCK(vp, (caddr_t)fp, F_SETLK, &lf, type)) != 0) {
2940 * lock request failed. Normally close the descriptor
2941 * but handle the case where someone might have dup()d
2942 * or close()d it when we weren't looking.
2944 if (fdp->fd_ofiles[indx] == fp) {
2945 fdp->fd_ofiles[indx] = NULL;
2950 * release our private reference.
2955 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY, td);
2956 fp->f_flag |= FHASLOCK;
2958 if ((vp->v_type == VREG) && (VOP_GETVOBJECT(vp, NULL) != 0))
2959 vfs_object_create(vp, td);
2961 VOP_UNLOCK(vp, 0, td);
2963 uap->sysmsg_result = indx;
2972 * fhstat_args(struct fhandle *u_fhp, struct stat *sb)
2975 fhstat(struct fhstat_args *uap)
2977 struct thread *td = curthread;
2985 * Must be super user
2991 error = copyin(SCARG(uap, u_fhp), &fh, sizeof(fhandle_t));
2995 if ((mp = vfs_getvfs(&fh.fh_fsid)) == NULL)
2997 if ((error = VFS_FHTOVP(mp, &fh.fh_fid, &vp)))
2999 error = vn_stat(vp, &sb, td);
3003 error = copyout(&sb, SCARG(uap, sb), sizeof(sb));
3008 * fhstatfs_args(struct fhandle *u_fhp, struct statfs *buf)
3011 fhstatfs(struct fhstatfs_args *uap)
3013 struct thread *td = curthread;
3022 * Must be super user
3024 if ((error = suser(td)))
3027 if ((error = copyin(SCARG(uap, u_fhp), &fh, sizeof(fhandle_t))) != 0)
3030 if ((mp = vfs_getvfs(&fh.fh_fsid)) == NULL)
3032 if ((error = VFS_FHTOVP(mp, &fh.fh_fid, &vp)))
3037 if ((error = VFS_STATFS(mp, sp, td)) != 0)
3039 sp->f_flags = mp->mnt_flag & MNT_VISFLAGMASK;
3041 bcopy((caddr_t)sp, (caddr_t)&sb, sizeof(sb));
3042 sb.f_fsid.val[0] = sb.f_fsid.val[1] = 0;
3045 return (copyout(sp, SCARG(uap, buf), sizeof(*sp)));
3049 * Syscall to push extended attribute configuration information into the
3050 * VFS. Accepts a path, which it converts to a mountpoint, as well as
3051 * a command (int cmd), and attribute name and misc data. For now, the
3052 * attribute name is left in userspace for consumption by the VFS_op.
3053 * It will probably be changed to be copied into sysspace by the
3054 * syscall in the future, once issues with various consumers of the
3055 * attribute code have raised their hands.
3057 * Currently this is used only by UFS Extended Attributes.
3060 extattrctl(struct extattrctl_args *uap)
3062 struct thread *td = curthread;
3063 struct nameidata nd;
3067 NDINIT(&nd, NAMEI_LOOKUP, CNP_FOLLOW, UIO_USERSPACE, SCARG(uap, path), td);
3068 if ((error = namei(&nd)) != 0)
3070 mp = nd.ni_vp->v_mount;
3072 return (VFS_EXTATTRCTL(mp, SCARG(uap, cmd), SCARG(uap, attrname),
3073 SCARG(uap, arg), td));
3077 * Syscall to set a named extended attribute on a file or directory.
3078 * Accepts attribute name, and a uio structure pointing to the data to set.
3079 * The uio is consumed in the style of writev(). The real work happens
3080 * in VOP_SETEXTATTR().
3083 extattr_set_file(struct extattr_set_file_args *uap)
3085 struct thread *td = curthread;
3086 struct proc *p = td->td_proc;
3087 struct nameidata nd;
3089 struct iovec *iov, *needfree = NULL, aiov[UIO_SMALLIOV];
3090 char attrname[EXTATTR_MAXNAMELEN];
3094 error = copyin(SCARG(uap, attrname), attrname, EXTATTR_MAXNAMELEN);
3097 NDINIT(&nd, NAMEI_LOOKUP, CNP_FOLLOW | CNP_LOCKLEAF, UIO_USERSPACE,
3098 SCARG(uap, path), td);
3099 if ((error = namei(&nd)) != 0)
3101 iovlen = uap->iovcnt * sizeof(struct iovec);
3102 if (uap->iovcnt > UIO_SMALLIOV) {
3103 if (uap->iovcnt > UIO_MAXIOV) {
3107 MALLOC(iov, struct iovec *, iovlen, M_IOV, M_WAITOK);
3112 auio.uio_iovcnt = uap->iovcnt;
3113 auio.uio_rw = UIO_WRITE;
3114 auio.uio_segflg = UIO_USERSPACE;
3116 auio.uio_offset = 0;
3117 if ((error = copyin((caddr_t)uap->iovp, (caddr_t)iov, iovlen)))
3120 for (i = 0; i < uap->iovcnt; i++) {
3121 if (iov->iov_len > INT_MAX - auio.uio_resid) {
3125 auio.uio_resid += iov->iov_len;
3128 cnt = auio.uio_resid;
3129 error = VOP_SETEXTATTR(nd.ni_vp, attrname, &auio, p->p_ucred, td);
3130 cnt -= auio.uio_resid;
3131 uap->sysmsg_result = cnt;
3134 FREE(needfree, M_IOV);
3140 * Syscall to get a named extended attribute on a file or directory.
3141 * Accepts attribute name, and a uio structure pointing to a buffer for the
3142 * data. The uio is consumed in the style of readv(). The real work
3143 * happens in VOP_GETEXTATTR();
3146 extattr_get_file(struct extattr_get_file_args *uap)
3148 struct thread *td = curthread;
3149 struct proc *p = td->td_proc;
3150 struct nameidata nd;
3152 struct iovec *iov, *needfree, aiov[UIO_SMALLIOV];
3153 char attrname[EXTATTR_MAXNAMELEN];
3157 error = copyin(SCARG(uap, attrname), attrname, EXTATTR_MAXNAMELEN);
3160 NDINIT(&nd, NAMEI_LOOKUP, CNP_FOLLOW | CNP_LOCKLEAF, UIO_USERSPACE,
3161 SCARG(uap, path), td);
3162 if ((error = namei(&nd)) != 0)
3164 iovlen = uap->iovcnt * sizeof (struct iovec);
3165 if (uap->iovcnt > UIO_SMALLIOV) {
3166 if (uap->iovcnt > UIO_MAXIOV) {
3170 MALLOC(iov, struct iovec *, iovlen, M_IOV, M_WAITOK);
3177 auio.uio_iovcnt = uap->iovcnt;
3178 auio.uio_rw = UIO_READ;
3179 auio.uio_segflg = UIO_USERSPACE;
3181 auio.uio_offset = 0;
3182 if ((error = copyin((caddr_t)uap->iovp, (caddr_t)iov, iovlen)))
3185 for (i = 0; i < uap->iovcnt; i++) {
3186 if (iov->iov_len > INT_MAX - auio.uio_resid) {
3190 auio.uio_resid += iov->iov_len;
3193 cnt = auio.uio_resid;
3194 error = VOP_GETEXTATTR(nd.ni_vp, attrname, &auio, p->p_ucred, td);
3195 cnt -= auio.uio_resid;
3196 uap->sysmsg_result = cnt;
3199 FREE(needfree, M_IOV);
3205 * Syscall to delete a named extended attribute from a file or directory.
3206 * Accepts attribute name. The real work happens in VOP_SETEXTATTR().
3209 extattr_delete_file(struct extattr_delete_file_args *uap)
3211 struct thread *td = curthread;
3212 struct proc *p = td->td_proc;
3213 struct nameidata nd;
3214 char attrname[EXTATTR_MAXNAMELEN];
3217 error = copyin(SCARG(uap, attrname), attrname, EXTATTR_MAXNAMELEN);
3220 NDINIT(&nd, NAMEI_LOOKUP, CNP_FOLLOW | CNP_LOCKLEAF, UIO_USERSPACE,
3221 SCARG(uap, path), td);
3222 if ((error = namei(&nd)) != 0)
3224 error = VOP_SETEXTATTR(nd.ni_vp, attrname, NULL, p->p_ucred, td);