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.29 2004/03/01 06:33:17 dillon Exp $
43 #include <sys/param.h>
44 #include <sys/systm.h>
46 #include <sys/sysent.h>
47 #include <sys/malloc.h>
48 #include <sys/mount.h>
49 #include <sys/sysproto.h>
50 #include <sys/filedesc.h>
51 #include <sys/kernel.h>
52 #include <sys/fcntl.h>
54 #include <sys/linker.h>
56 #include <sys/unistd.h>
57 #include <sys/vnode.h>
59 #include <sys/namei.h>
60 #include <sys/dirent.h>
61 #include <sys/extattr.h>
62 #include <sys/kern_syscall.h>
64 #include <machine/limits.h>
65 #include <vfs/union/union.h>
66 #include <sys/sysctl.h>
68 #include <vm/vm_object.h>
69 #include <vm/vm_zone.h>
70 #include <vm/vm_page.h>
72 #include <sys/file2.h>
74 static int change_dir (struct nameidata *ndp, struct thread *td);
75 static void checkdirs (struct vnode *olddp);
76 static int chroot_refuse_vdir_fds (struct filedesc *fdp);
77 static int getutimes (const struct timeval *, struct timespec *);
78 static int setfown (struct vnode *, uid_t, gid_t);
79 static int setfmode (struct vnode *, int);
80 static int setfflags (struct vnode *, int);
81 static int setutimes (struct vnode *, const struct timespec *, int);
82 static int usermount = 0; /* if 1, non-root can mount fs. */
84 int (*union_dircheckp) (struct thread *, struct vnode **, struct file *);
86 SYSCTL_INT(_vfs, OID_AUTO, usermount, CTLFLAG_RW, &usermount, 0, "");
89 * Virtual File System System Calls
93 * Mount a file system.
96 * mount_args(char *type, char *path, int flags, caddr_t data)
100 mount(struct mount_args *uap)
102 struct thread *td = curthread;
103 struct proc *p = td->td_proc;
106 struct vfsconf *vfsp;
107 int error, flag = 0, flag2 = 0;
110 char fstypename[MFSNAMELEN];
114 if (usermount == 0 && (error = suser(td)))
117 * Do not allow NFS export by non-root users.
119 if (SCARG(uap, flags) & MNT_EXPORTED) {
125 * Silently enforce MNT_NOSUID and MNT_NODEV for non-root users
128 SCARG(uap, flags) |= MNT_NOSUID | MNT_NODEV;
130 * Get vnode to be covered
132 NDINIT(&nd, NAMEI_LOOKUP, CNP_FOLLOW | CNP_LOCKLEAF, UIO_USERSPACE,
133 SCARG(uap, path), td);
134 if ((error = namei(&nd)) != 0)
136 NDFREE(&nd, NDF_ONLY_PNBUF);
138 if (SCARG(uap, flags) & MNT_UPDATE) {
139 if ((vp->v_flag & VROOT) == 0) {
145 flag2 = mp->mnt_kern_flag;
147 * We only allow the filesystem to be reloaded if it
148 * is currently mounted read-only.
150 if ((SCARG(uap, flags) & MNT_RELOAD) &&
151 ((mp->mnt_flag & MNT_RDONLY) == 0)) {
153 return (EOPNOTSUPP); /* Needs translation */
156 * Only root, or the user that did the original mount is
157 * permitted to update it.
159 if (mp->mnt_stat.f_owner != p->p_ucred->cr_uid &&
160 (error = suser(td))) {
164 if (vfs_busy(mp, LK_NOWAIT, NULL, td)) {
168 lwkt_gettoken(&vlock, vp->v_interlock);
169 if ((vp->v_flag & VMOUNT) != 0 ||
170 vp->v_mountedhere != NULL) {
171 lwkt_reltoken(&vlock);
176 vp->v_flag |= VMOUNT;
177 lwkt_reltoken(&vlock);
179 SCARG(uap, flags) & (MNT_RELOAD | MNT_FORCE | MNT_UPDATE);
180 VOP_UNLOCK(vp, NULL, 0, td);
184 * If the user is not root, ensure that they own the directory
185 * onto which we are attempting to mount.
187 if ((error = VOP_GETATTR(vp, &va, td)) ||
188 (va.va_uid != p->p_ucred->cr_uid &&
189 (error = suser(td)))) {
193 if ((error = vinvalbuf(vp, V_SAVE, td, 0, 0)) != 0) {
197 if (vp->v_type != VDIR) {
201 if ((error = copyinstr(SCARG(uap, type), fstypename, MFSNAMELEN, NULL)) != 0) {
205 for (vfsp = vfsconf; vfsp; vfsp = vfsp->vfc_next)
206 if (!strcmp(vfsp->vfc_name, fstypename))
211 /* Only load modules for root (very important!) */
212 if ((error = suser(td)) != 0) {
216 error = linker_load_file(fstypename, &lf);
217 if (error || lf == NULL) {
224 /* lookup again, see if the VFS was loaded */
225 for (vfsp = vfsconf; vfsp; vfsp = vfsp->vfc_next)
226 if (!strcmp(vfsp->vfc_name, fstypename))
230 linker_file_unload(lf);
235 lwkt_gettoken(&vlock, vp->v_interlock);
236 if ((vp->v_flag & VMOUNT) != 0 ||
237 vp->v_mountedhere != NULL) {
238 lwkt_reltoken(&vlock);
242 vp->v_flag |= VMOUNT;
243 lwkt_reltoken(&vlock);
246 * Allocate and initialize the filesystem.
248 mp = malloc(sizeof(struct mount), M_MOUNT, M_WAITOK);
249 bzero((char *)mp, (u_long)sizeof(struct mount));
250 TAILQ_INIT(&mp->mnt_nvnodelist);
251 TAILQ_INIT(&mp->mnt_reservedvnlist);
252 mp->mnt_nvnodelistsize = 0;
253 lockinit(&mp->mnt_lock, 0, "vfslock", 0, LK_NOPAUSE);
254 vfs_busy(mp, LK_NOWAIT, NULL, td);
255 mp->mnt_op = vfsp->vfc_vfsops;
257 vfsp->vfc_refcount++;
258 mp->mnt_stat.f_type = vfsp->vfc_typenum;
259 mp->mnt_flag |= vfsp->vfc_flags & MNT_VISFLAGMASK;
260 strncpy(mp->mnt_stat.f_fstypename, vfsp->vfc_name, MFSNAMELEN);
261 mp->mnt_vnodecovered = vp;
262 mp->mnt_stat.f_owner = p->p_ucred->cr_uid;
263 mp->mnt_iosize_max = DFLTPHYS;
264 VOP_UNLOCK(vp, NULL, 0, td);
267 * Set the mount level flags.
269 if (SCARG(uap, flags) & MNT_RDONLY)
270 mp->mnt_flag |= MNT_RDONLY;
271 else if (mp->mnt_flag & MNT_RDONLY)
272 mp->mnt_kern_flag |= MNTK_WANTRDWR;
273 mp->mnt_flag &=~ (MNT_NOSUID | MNT_NOEXEC | MNT_NODEV |
274 MNT_SYNCHRONOUS | MNT_UNION | MNT_ASYNC | MNT_NOATIME |
275 MNT_NOSYMFOLLOW | MNT_IGNORE |
276 MNT_NOCLUSTERR | MNT_NOCLUSTERW | MNT_SUIDDIR);
277 mp->mnt_flag |= SCARG(uap, flags) & (MNT_NOSUID | MNT_NOEXEC |
278 MNT_NODEV | MNT_SYNCHRONOUS | MNT_UNION | MNT_ASYNC | MNT_FORCE |
279 MNT_NOSYMFOLLOW | MNT_IGNORE |
280 MNT_NOATIME | MNT_NOCLUSTERR | MNT_NOCLUSTERW | MNT_SUIDDIR);
282 * Mount the filesystem.
283 * XXX The final recipients of VFS_MOUNT just overwrite the ndp they
284 * get. No freeing of cn_pnbuf.
286 error = VFS_MOUNT(mp, SCARG(uap, path), SCARG(uap, data), &nd, td);
287 if (mp->mnt_flag & MNT_UPDATE) {
288 if (mp->mnt_kern_flag & MNTK_WANTRDWR)
289 mp->mnt_flag &= ~MNT_RDONLY;
290 mp->mnt_flag &=~ (MNT_UPDATE | MNT_RELOAD | MNT_FORCE);
291 mp->mnt_kern_flag &=~ MNTK_WANTRDWR;
294 mp->mnt_kern_flag = flag2;
297 lwkt_gettoken(&vlock, vp->v_interlock);
298 vp->v_flag &= ~VMOUNT;
299 lwkt_reltoken(&vlock);
303 vn_lock(vp, NULL, LK_EXCLUSIVE | LK_RETRY, td);
305 * Put the new filesystem on the mount list after root.
309 lwkt_gettoken(&vlock, vp->v_interlock);
310 vp->v_flag &= ~VMOUNT;
311 vp->v_mountedhere = mp;
312 lwkt_reltoken(&vlock);
313 lwkt_gettoken(&ilock, &mountlist_token);
314 TAILQ_INSERT_TAIL(&mountlist, mp, mnt_list);
315 lwkt_reltoken(&ilock);
317 VOP_UNLOCK(vp, NULL, 0, td);
318 error = vfs_allocate_syncvnode(mp);
320 if ((error = VFS_START(mp, 0, td)) != 0)
323 lwkt_gettoken(&vlock, vp->v_interlock);
324 vp->v_flag &= ~VMOUNT;
325 lwkt_reltoken(&vlock);
326 mp->mnt_vfc->vfc_refcount--;
328 free((caddr_t)mp, M_MOUNT);
335 * Scan all active processes to see if any of them have a current
336 * or root directory onto which the new filesystem has just been
337 * mounted. If so, replace them with the new mount point.
340 checkdirs(struct vnode *olddp)
342 struct filedesc *fdp;
346 if (olddp->v_usecount == 1)
348 if (VFS_ROOT(olddp->v_mountedhere, &newdp))
349 panic("mount: lost mount");
350 FOREACH_PROC_IN_SYSTEM(p) {
352 if (fdp->fd_cdir == olddp) {
355 fdp->fd_cdir = newdp;
357 if (fdp->fd_rdir == olddp) {
360 fdp->fd_rdir = newdp;
363 if (rootvnode == olddp) {
367 vfs_cache_setroot(rootvnode);
373 * Unmount a file system.
375 * Note: unmount takes a path to the vnode mounted on as argument,
376 * not special file (as before).
379 * umount_args(char *path, int flags)
383 unmount(struct unmount_args *uap)
385 struct thread *td = curthread;
386 struct proc *p = td->td_proc;
393 NDINIT(&nd, NAMEI_LOOKUP, CNP_FOLLOW | CNP_LOCKLEAF, UIO_USERSPACE,
394 SCARG(uap, path), td);
395 if ((error = namei(&nd)) != 0)
398 NDFREE(&nd, NDF_ONLY_PNBUF);
402 * Only root, or the user that did the original mount is
403 * permitted to unmount this filesystem.
405 if ((mp->mnt_stat.f_owner != p->p_ucred->cr_uid) &&
406 (error = suser(td))) {
412 * Don't allow unmounting the root file system.
414 if (mp->mnt_flag & MNT_ROOTFS) {
420 * Must be the root of the filesystem
422 if ((vp->v_flag & VROOT) == 0) {
427 return (dounmount(mp, SCARG(uap, flags), td));
431 * Do the actual file system unmount.
434 dounmount(struct mount *mp, int flags, struct thread *td)
436 struct vnode *coveredvp;
441 lwkt_gettoken(&ilock, &mountlist_token);
442 if (mp->mnt_kern_flag & MNTK_UNMOUNT) {
443 lwkt_reltoken(&ilock);
446 mp->mnt_kern_flag |= MNTK_UNMOUNT;
447 /* Allow filesystems to detect that a forced unmount is in progress. */
448 if (flags & MNT_FORCE)
449 mp->mnt_kern_flag |= MNTK_UNMOUNTF;
450 error = lockmgr(&mp->mnt_lock, LK_DRAIN | LK_INTERLOCK |
451 ((flags & MNT_FORCE) ? 0 : LK_NOWAIT), &ilock, td);
453 mp->mnt_kern_flag &= ~(MNTK_UNMOUNT | MNTK_UNMOUNTF);
454 if (mp->mnt_kern_flag & MNTK_MWAIT)
459 if (mp->mnt_flag & MNT_EXPUBLIC)
460 vfs_setpublicfs(NULL, NULL, NULL);
462 vfs_msync(mp, MNT_WAIT);
463 async_flag = mp->mnt_flag & MNT_ASYNC;
464 mp->mnt_flag &=~ MNT_ASYNC;
465 cache_purgevfs(mp); /* remove cache entries for this file sys */
466 if (mp->mnt_syncer != NULL)
467 vrele(mp->mnt_syncer);
468 if (((mp->mnt_flag & MNT_RDONLY) ||
469 (error = VFS_SYNC(mp, MNT_WAIT, td)) == 0) ||
471 error = VFS_UNMOUNT(mp, flags, td);
472 lwkt_gettokref(&ilock);
474 if (mp->mnt_syncer == NULL)
475 vfs_allocate_syncvnode(mp);
476 mp->mnt_kern_flag &= ~(MNTK_UNMOUNT | MNTK_UNMOUNTF);
477 mp->mnt_flag |= async_flag;
478 lockmgr(&mp->mnt_lock, LK_RELEASE | LK_INTERLOCK | LK_REENABLE,
480 if (mp->mnt_kern_flag & MNTK_MWAIT)
484 TAILQ_REMOVE(&mountlist, mp, mnt_list);
485 if ((coveredvp = mp->mnt_vnodecovered) != NULLVP) {
486 coveredvp->v_mountedhere = NULL;
489 mp->mnt_vfc->vfc_refcount--;
490 if (!TAILQ_EMPTY(&mp->mnt_nvnodelist))
491 panic("unmount: dangling vnode");
492 lockmgr(&mp->mnt_lock, LK_RELEASE | LK_INTERLOCK, &ilock, td);
493 if (mp->mnt_kern_flag & MNTK_MWAIT)
495 free((caddr_t)mp, M_MOUNT);
500 * Sync each mounted filesystem.
504 static int syncprt = 0;
505 SYSCTL_INT(_debug, OID_AUTO, syncprt, CTLFLAG_RW, &syncprt, 0, "");
510 sync(struct sync_args *uap)
512 struct thread *td = curthread;
513 struct mount *mp, *nmp;
517 lwkt_gettoken(&ilock, &mountlist_token);
518 for (mp = TAILQ_FIRST(&mountlist); mp != NULL; mp = nmp) {
519 if (vfs_busy(mp, LK_NOWAIT, &ilock, td)) {
520 nmp = TAILQ_NEXT(mp, mnt_list);
523 if ((mp->mnt_flag & MNT_RDONLY) == 0) {
524 asyncflag = mp->mnt_flag & MNT_ASYNC;
525 mp->mnt_flag &= ~MNT_ASYNC;
526 vfs_msync(mp, MNT_NOWAIT);
527 VFS_SYNC(mp, MNT_NOWAIT, td);
528 mp->mnt_flag |= asyncflag;
530 lwkt_gettokref(&ilock);
531 nmp = TAILQ_NEXT(mp, mnt_list);
534 lwkt_reltoken(&ilock);
537 * XXX don't call vfs_bufstats() yet because that routine
538 * was not imported in the Lite2 merge.
543 #endif /* DIAGNOSTIC */
548 /* XXX PRISON: could be per prison flag */
549 static int prison_quotas;
551 SYSCTL_INT(_kern_prison, OID_AUTO, quotas, CTLFLAG_RW, &prison_quotas, 0, "");
555 * quotactl_args(char *path, int fcmd, int uid, caddr_t arg)
557 * Change filesystem quotas.
561 quotactl(struct quotactl_args *uap)
563 struct thread *td = curthread;
564 struct proc *p = td->td_proc;
570 if (p->p_ucred->cr_prison && !prison_quotas)
572 NDINIT(&nd, NAMEI_LOOKUP, CNP_FOLLOW, UIO_USERSPACE,
573 SCARG(uap, path), td);
574 if ((error = namei(&nd)) != 0)
576 mp = nd.ni_vp->v_mount;
577 NDFREE(&nd, NDF_ONLY_PNBUF);
579 return (VFS_QUOTACTL(mp, SCARG(uap, cmd), SCARG(uap, uid),
580 SCARG(uap, arg), td));
584 kern_statfs(struct nameidata *nd, struct statfs *buf)
586 struct thread *td = curthread;
594 mp = nd->ni_vp->v_mount;
596 NDFREE(nd, NDF_ONLY_PNBUF);
598 error = VFS_STATFS(mp, sp, td);
601 sp->f_flags = mp->mnt_flag & MNT_VISFLAGMASK;
602 bcopy(sp, buf, sizeof(*buf));
603 /* Only root should have access to the fsid's. */
605 buf->f_fsid.val[0] = buf->f_fsid.val[1] = 0;
610 * statfs_args(char *path, struct statfs *buf)
612 * Get filesystem statistics.
615 statfs(struct statfs_args *uap)
617 struct thread *td = curthread;
622 NDINIT(&nd, NAMEI_LOOKUP, CNP_FOLLOW, UIO_USERSPACE, uap->path, td);
624 error = kern_statfs(&nd, &buf);
627 error = copyout(&buf, uap->buf, sizeof(*uap->buf));
632 kern_fstatfs(int fd, struct statfs *buf)
634 struct thread *td = curthread;
635 struct proc *p = td->td_proc;
642 error = getvnode(p->p_fd, fd, &fp);
645 mp = ((struct vnode *)fp->f_data)->v_mount;
649 error = VFS_STATFS(mp, sp, td);
652 sp->f_flags = mp->mnt_flag & MNT_VISFLAGMASK;
653 bcopy(sp, buf, sizeof(*buf));
654 /* Only root should have access to the fsid's. */
656 buf->f_fsid.val[0] = buf->f_fsid.val[1] = 0;
661 * fstatfs_args(int fd, struct statfs *buf)
663 * Get filesystem statistics.
666 fstatfs(struct fstatfs_args *uap)
671 error = kern_fstatfs(uap->fd, &buf);
674 error = copyout(&buf, uap->buf, sizeof(*uap->buf));
679 * getfsstat_args(struct statfs *buf, long bufsize, int flags)
681 * Get statistics on all filesystems.
685 getfsstat(struct getfsstat_args *uap)
687 struct thread *td = curthread;
688 struct mount *mp, *nmp;
692 long count, maxcount, error;
694 maxcount = SCARG(uap, bufsize) / sizeof(struct statfs);
695 sfsp = (caddr_t)SCARG(uap, buf);
697 lwkt_gettoken(&ilock, &mountlist_token);
698 for (mp = TAILQ_FIRST(&mountlist); mp != NULL; mp = nmp) {
699 if (vfs_busy(mp, LK_NOWAIT, &ilock, td)) {
700 nmp = TAILQ_NEXT(mp, mnt_list);
703 if (sfsp && count < maxcount) {
706 * If MNT_NOWAIT or MNT_LAZY is specified, do not
707 * refresh the fsstat cache. MNT_NOWAIT or MNT_LAZY
708 * overrides MNT_WAIT.
710 if (((SCARG(uap, flags) & (MNT_LAZY|MNT_NOWAIT)) == 0 ||
711 (SCARG(uap, flags) & MNT_WAIT)) &&
712 (error = VFS_STATFS(mp, sp, td))) {
713 lwkt_gettokref(&ilock);
714 nmp = TAILQ_NEXT(mp, mnt_list);
718 sp->f_flags = mp->mnt_flag & MNT_VISFLAGMASK;
719 error = copyout((caddr_t)sp, sfsp, sizeof(*sp));
727 lwkt_gettokref(&ilock);
728 nmp = TAILQ_NEXT(mp, mnt_list);
731 lwkt_reltoken(&ilock);
732 if (sfsp && count > maxcount)
733 uap->sysmsg_result = maxcount;
735 uap->sysmsg_result = count;
740 * fchdir_args(int fd)
742 * Change current working directory to a given file descriptor.
746 fchdir(struct fchdir_args *uap)
748 struct thread *td = curthread;
749 struct proc *p = td->td_proc;
750 struct filedesc *fdp = p->p_fd;
751 struct vnode *vp, *tdp;
756 if ((error = getvnode(fdp, SCARG(uap, fd), &fp)) != 0)
758 vp = (struct vnode *)fp->f_data;
760 vn_lock(vp, NULL, LK_EXCLUSIVE | LK_RETRY, td);
761 if (vp->v_type != VDIR)
764 error = VOP_ACCESS(vp, VEXEC, p->p_ucred, td);
765 while (!error && (mp = vp->v_mountedhere) != NULL) {
766 if (vfs_busy(mp, 0, NULL, td))
768 error = VFS_ROOT(mp, &tdp);
779 VOP_UNLOCK(vp, NULL, 0, td);
786 kern_chdir(struct nameidata *nd)
788 struct thread *td = curthread;
789 struct proc *p = td->td_proc;
790 struct filedesc *fdp = p->p_fd;
793 error = change_dir(nd, td);
796 NDFREE(nd, NDF_ONLY_PNBUF);
798 fdp->fd_cdir = nd->ni_vp;
803 * chdir_args(char *path)
805 * Change current working directory (``.'').
808 chdir(struct chdir_args *uap)
810 struct thread *td = curthread;
814 NDINIT(&nd, NAMEI_LOOKUP, CNP_FOLLOW | CNP_LOCKLEAF, UIO_USERSPACE,
817 error = kern_chdir(&nd);
823 * Helper function for raised chroot(2) security function: Refuse if
824 * any filedescriptors are open directories.
827 chroot_refuse_vdir_fds(fdp)
828 struct filedesc *fdp;
835 for (fd = 0; fd < fdp->fd_nfiles ; fd++) {
836 error = getvnode(fdp, fd, &fp);
839 vp = (struct vnode *)fp->f_data;
840 if (vp->v_type != VDIR)
848 * This sysctl determines if we will allow a process to chroot(2) if it
849 * has a directory open:
850 * 0: disallowed for all processes.
851 * 1: allowed for processes that were not already chroot(2)'ed.
852 * 2: allowed for all processes.
855 static int chroot_allow_open_directories = 1;
857 SYSCTL_INT(_kern, OID_AUTO, chroot_allow_open_directories, CTLFLAG_RW,
858 &chroot_allow_open_directories, 0, "");
861 * chroot_args(char *path)
863 * Change notion of root (``/'') directory.
867 chroot(struct chroot_args *uap)
869 struct thread *td = curthread;
870 struct proc *p = td->td_proc;
871 struct filedesc *fdp = p->p_fd;
876 error = suser_cred(p->p_ucred, PRISON_ROOT);
879 if (chroot_allow_open_directories == 0 ||
880 (chroot_allow_open_directories == 1 && fdp->fd_rdir != rootvnode))
881 error = chroot_refuse_vdir_fds(fdp);
884 NDINIT(&nd, NAMEI_LOOKUP, CNP_FOLLOW | CNP_LOCKLEAF, UIO_USERSPACE,
885 SCARG(uap, path), td);
886 if ((error = change_dir(&nd, td)) != 0)
888 NDFREE(&nd, NDF_ONLY_PNBUF);
890 fdp->fd_rdir = nd.ni_vp;
892 fdp->fd_jdir = nd.ni_vp;
899 * Common routine for chroot and chdir.
902 change_dir(struct nameidata *ndp, struct thread *td)
911 if (vp->v_type != VDIR)
914 error = VOP_ACCESS(vp, VEXEC, ndp->ni_cnd.cn_cred, td);
918 VOP_UNLOCK(vp, NULL, 0, td);
923 kern_open(struct nameidata *nd, int oflags, int mode, int *res)
925 struct thread *td = curthread;
926 struct proc *p = td->td_proc;
927 struct filedesc *fdp = p->p_fd;
932 int type, indx, error;
935 if ((oflags & O_ACCMODE) == O_ACCMODE)
937 flags = FFLAGS(oflags);
938 error = falloc(p, &nfp, &indx);
942 cmode = ((mode &~ fdp->fd_cmask) & ALLPERMS) &~ S_ISTXT;
943 p->p_dupfd = -indx - 1; /* XXX check for fdopen */
945 * Bump the ref count to prevent another process from closing
946 * the descriptor while we are blocked in vn_open()
949 error = vn_open(nd, flags, cmode);
952 * release our own reference
957 * handle special fdopen() case. bleh. dupfdopen() is
958 * responsible for dropping the old contents of ofiles[indx]
961 if ((error == ENODEV || error == ENXIO) &&
962 p->p_dupfd >= 0 && /* XXX from fdopen */
964 dupfdopen(fdp, indx, p->p_dupfd, flags, error)) == 0) {
969 * Clean up the descriptor, but only if another thread hadn't
970 * replaced or closed it.
972 if (fdp->fd_ofiles[indx] == fp) {
973 fdp->fd_ofiles[indx] = NULL;
977 if (error == ERESTART)
982 NDFREE(nd, NDF_ONLY_PNBUF);
986 * There should be 2 references on the file, one from the descriptor
987 * table, and one for us.
989 * Handle the case where someone closed the file (via its file
990 * descriptor) while we were blocked. The end result should look
991 * like opening the file succeeded but it was immediately closed.
993 if (fp->f_count == 1) {
994 KASSERT(fdp->fd_ofiles[indx] != fp,
995 ("Open file descriptor lost all refs"));
996 VOP_UNLOCK(vp, NULL, 0, td);
997 vn_close(vp, flags & FMASK, td);
1003 fp->f_data = (caddr_t)vp;
1004 fp->f_flag = flags & FMASK;
1006 fp->f_type = (vp->v_type == VFIFO ? DTYPE_FIFO : DTYPE_VNODE);
1007 if (flags & (O_EXLOCK | O_SHLOCK)) {
1008 lf.l_whence = SEEK_SET;
1011 if (flags & O_EXLOCK)
1012 lf.l_type = F_WRLCK;
1014 lf.l_type = F_RDLCK;
1016 if ((flags & FNONBLOCK) == 0)
1018 VOP_UNLOCK(vp, NULL, 0, td);
1019 if ((error = VOP_ADVLOCK(vp, (caddr_t)fp, F_SETLK, &lf, type)) != 0) {
1021 * lock request failed. Normally close the descriptor
1022 * but handle the case where someone might have dup()d
1023 * it when we weren't looking. One reference is
1024 * owned by the descriptor array, the other by us.
1026 if (fdp->fd_ofiles[indx] == fp) {
1027 fdp->fd_ofiles[indx] = NULL;
1033 vn_lock(vp, NULL, LK_EXCLUSIVE | LK_RETRY, td);
1034 fp->f_flag |= FHASLOCK;
1036 /* assert that vn_open created a backing object if one is needed */
1037 KASSERT(!vn_canvmio(vp) || VOP_GETVOBJECT(vp, NULL) == 0,
1038 ("open: vmio vnode has no backing object after vn_open"));
1039 VOP_UNLOCK(vp, NULL, 0, td);
1042 * release our private reference, leaving the one associated with the
1043 * descriptor table intact.
1051 * open_args(char *path, int flags, int mode)
1053 * Check permissions, allocate an open file structure,
1054 * and call the device open routine if any.
1057 open(struct open_args *uap)
1059 struct thread *td = curthread;
1060 struct nameidata nd;
1063 NDINIT(&nd, NAMEI_LOOKUP, CNP_FOLLOW, UIO_USERSPACE, uap->path, td);
1065 error = kern_open(&nd, uap->flags, uap->mode, &uap->sysmsg_result);
1071 kern_mknod(struct nameidata *nd, int mode, int dev)
1073 struct thread *td = curthread;
1074 struct proc *p = td->td_proc;
1082 switch (mode & S_IFMT) {
1088 error = suser_cred(p->p_ucred, PRISON_ROOT);
1102 vattr.va_mode = (mode & ALLPERMS) &~ p->p_fd->fd_cmask;
1103 vattr.va_rdev = dev;
1106 switch (mode & S_IFMT) {
1107 case S_IFMT: /* used by badsect to flag bad sectors */
1108 vattr.va_type = VBAD;
1111 vattr.va_type = VCHR;
1114 vattr.va_type = VBLK;
1125 VOP_LEASE(nd->ni_dvp, td, p->p_ucred, LEASE_WRITE);
1127 error = VOP_WHITEOUT(nd->ni_dvp, NCPNULL,
1128 &nd->ni_cnd, NAMEI_CREATE);
1130 error = VOP_MKNOD(nd->ni_dvp, NCPNULL, &nd->ni_vp,
1131 &nd->ni_cnd, &vattr);
1135 NDFREE(nd, NDF_ONLY_PNBUF);
1138 NDFREE(nd, NDF_ONLY_PNBUF);
1139 if (nd->ni_dvp == vp)
1146 ASSERT_VOP_UNLOCKED(nd->ni_dvp, "mknod");
1147 ASSERT_VOP_UNLOCKED(nd->ni_vp, "mknod");
1152 * mknod_args(char *path, int mode, int dev)
1154 * Create a special file.
1157 mknod(struct mknod_args *uap)
1159 struct thread *td = curthread;
1160 struct nameidata nd;
1163 NDINIT(&nd, NAMEI_CREATE, CNP_LOCKPARENT, UIO_USERSPACE, uap->path,
1166 error = kern_mknod(&nd, uap->mode, uap->dev);
1172 kern_mkfifo(struct nameidata *nd, int mode)
1174 struct thread *td = curthread;
1175 struct proc *p = td->td_proc;
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 = (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 * mkfifo_args(char *path, int mode)
1207 * Create a named pipe.
1210 mkfifo(struct mkfifo_args *uap)
1212 struct thread *td = curthread;
1213 struct nameidata nd;
1216 NDINIT(&nd, NAMEI_CREATE, CNP_LOCKPARENT, UIO_USERSPACE, uap->path,
1219 error = kern_mkfifo(&nd, uap->mode);
1225 kern_link(struct nameidata *nd, struct nameidata *linknd)
1227 struct thread *td = curthread;
1228 struct proc *p = td->td_proc;
1236 NDFREE(nd, NDF_ONLY_PNBUF);
1238 if (vp->v_type == VDIR)
1239 error = EPERM; /* POSIX */
1241 error = namei(linknd);
1243 if (linknd->ni_vp != NULL) {
1245 vrele(linknd->ni_vp);
1248 VOP_LEASE(linknd->ni_dvp, td, p->p_ucred,
1250 VOP_LEASE(vp, td, p->p_ucred, LEASE_WRITE);
1251 error = VOP_LINK(linknd->ni_dvp, NCPNULL, vp,
1254 NDFREE(linknd, NDF_ONLY_PNBUF);
1255 if (linknd->ni_dvp == linknd->ni_vp)
1256 vrele(linknd->ni_dvp);
1258 vput(linknd->ni_dvp);
1259 ASSERT_VOP_UNLOCKED(linknd->ni_dvp, "link");
1260 ASSERT_VOP_UNLOCKED(linknd->ni_vp, "link");
1268 * link_args(char *path, char *link)
1270 * Make a hard file link.
1273 link(struct link_args *uap)
1275 struct thread *td = curthread;
1276 struct nameidata nd, linknd;
1279 NDINIT(&nd, NAMEI_LOOKUP, CNP_FOLLOW | CNP_NOOBJ, UIO_USERSPACE,
1281 NDINIT(&linknd, NAMEI_CREATE, CNP_LOCKPARENT | CNP_NOOBJ,
1282 UIO_USERSPACE, uap->link, td);
1284 error = kern_link(&nd, &linknd);
1290 kern_symlink(char *path, struct nameidata *nd)
1292 struct thread *td = curthread;
1293 struct proc *p = td->td_proc;
1302 NDFREE(nd, NDF_ONLY_PNBUF);
1303 if (nd->ni_dvp == nd->ni_vp)
1311 vattr.va_mode = ACCESSPERMS &~ p->p_fd->fd_cmask;
1312 VOP_LEASE(nd->ni_dvp, td, p->p_ucred, LEASE_WRITE);
1313 error = VOP_SYMLINK(nd->ni_dvp, NCPNULL, &nd->ni_vp, &nd->ni_cnd,
1315 NDFREE(nd, NDF_ONLY_PNBUF);
1319 ASSERT_VOP_UNLOCKED(nd->ni_dvp, "symlink");
1320 ASSERT_VOP_UNLOCKED(nd->ni_vp, "symlink");
1326 * symlink(char *path, char *link)
1328 * Make a symbolic link.
1331 symlink(struct symlink_args *uap)
1333 struct thread *td = curthread;
1334 struct nameidata nd;
1338 path = zalloc(namei_zone);
1339 error = copyinstr(uap->path, path, MAXPATHLEN, NULL);
1342 NDINIT(&nd, NAMEI_CREATE, CNP_LOCKPARENT | CNP_NOOBJ, UIO_USERSPACE,
1345 error = kern_symlink(path, &nd);
1347 zfree(namei_zone, path);
1352 * undelete_args(char *path)
1354 * Delete a whiteout from the filesystem.
1358 undelete(struct undelete_args *uap)
1360 struct thread *td = curthread;
1361 struct proc *p = td->td_proc;
1363 struct nameidata nd;
1366 NDINIT(&nd, NAMEI_DELETE, CNP_LOCKPARENT | CNP_DOWHITEOUT, UIO_USERSPACE,
1367 SCARG(uap, path), td);
1372 if (nd.ni_vp != NULLVP || !(nd.ni_cnd.cn_flags & CNP_ISWHITEOUT)) {
1373 NDFREE(&nd, NDF_ONLY_PNBUF);
1374 if (nd.ni_dvp == nd.ni_vp)
1383 VOP_LEASE(nd.ni_dvp, td, p->p_ucred, LEASE_WRITE);
1384 error = VOP_WHITEOUT(nd.ni_dvp, NCPNULL, &nd.ni_cnd, NAMEI_DELETE);
1385 NDFREE(&nd, NDF_ONLY_PNBUF);
1387 ASSERT_VOP_UNLOCKED(nd.ni_dvp, "undelete");
1388 ASSERT_VOP_UNLOCKED(nd.ni_vp, "undelete");
1393 kern_unlink(struct nameidata *nd)
1395 struct thread *td = curthread;
1396 struct proc *p = td->td_proc;
1405 VOP_LEASE(vp, td, p->p_ucred, LEASE_WRITE);
1406 vn_lock(vp, NULL, LK_EXCLUSIVE | LK_RETRY, td);
1408 if (vp->v_type == VDIR)
1409 error = EPERM; /* POSIX */
1412 * The root of a mounted filesystem cannot be deleted.
1414 * XXX: can this only be a VDIR case?
1416 if (vp->v_flag & VROOT)
1421 VOP_LEASE(nd->ni_dvp, td, p->p_ucred, LEASE_WRITE);
1422 error = VOP_REMOVE(nd->ni_dvp, NCPNULL, vp, &nd->ni_cnd);
1424 NDFREE(nd, NDF_ONLY_PNBUF);
1425 if (nd->ni_dvp == vp)
1431 ASSERT_VOP_UNLOCKED(nd->ni_dvp, "unlink");
1432 ASSERT_VOP_UNLOCKED(nd->ni_vp, "unlink");
1437 * unlink_args(char *path)
1439 * Delete a name from the filesystem.
1442 unlink(struct unlink_args *uap)
1444 struct thread *td = curthread;
1445 struct nameidata nd;
1448 NDINIT(&nd, NAMEI_DELETE, CNP_LOCKPARENT, UIO_USERSPACE, uap->path,
1451 error = kern_unlink(&nd);
1457 kern_lseek(int fd, off_t offset, int whence, off_t *res)
1459 struct thread *td = curthread;
1460 struct proc *p = td->td_proc;
1461 struct filedesc *fdp = p->p_fd;
1466 if (fd >= fdp->fd_nfiles ||
1467 (fp = fdp->fd_ofiles[fd]) == NULL)
1469 if (fp->f_type != DTYPE_VNODE)
1473 fp->f_offset += offset;
1476 error=VOP_GETATTR((struct vnode *)fp->f_data, &vattr, td);
1479 fp->f_offset = offset + vattr.va_size;
1482 fp->f_offset = offset;
1487 *res = fp->f_offset;
1492 * lseek_args(int fd, int pad, off_t offset, int whence)
1494 * Reposition read/write file offset.
1497 lseek(struct lseek_args *uap)
1501 error = kern_lseek(uap->fd, uap->offset, uap->whence,
1502 &uap->sysmsg_offset);
1508 kern_access(struct nameidata *nd, int aflags)
1510 struct thread *td = curthread;
1511 struct proc *p = td->td_proc;
1512 struct ucred *cred, *tmpcred;
1518 * Create and modify a temporary credential instead of one that
1519 * is potentially shared. This could also mess up socket
1520 * buffer accounting which can run in an interrupt context.
1522 tmpcred = crdup(cred);
1523 tmpcred->cr_uid = p->p_ucred->cr_ruid;
1524 tmpcred->cr_groups[0] = p->p_ucred->cr_rgid;
1525 p->p_ucred = tmpcred;
1526 nd->ni_cnd.cn_cred = tmpcred;
1532 /* Flags == 0 means only check for existence. */
1541 if ((flags & VWRITE) == 0 || (error = vn_writechk(vp)) == 0)
1542 error = VOP_ACCESS(vp, flags, tmpcred, td);
1544 NDFREE(nd, NDF_ONLY_PNBUF);
1553 * access_args(char *path, int flags)
1555 * Check access permissions.
1558 access(struct access_args *uap)
1560 struct thread *td = curthread;
1561 struct nameidata nd;
1564 NDINIT(&nd, NAMEI_LOOKUP, CNP_FOLLOW | CNP_LOCKLEAF | CNP_NOOBJ,
1565 UIO_USERSPACE, uap->path, td);
1567 error = kern_access(&nd, uap->flags);
1573 kern_stat(struct nameidata *nd, struct stat *st)
1575 struct thread *td = curthread;
1581 error = vn_stat(nd->ni_vp, st, td);
1582 NDFREE(nd, NDF_ONLY_PNBUF);
1588 * stat_args(char *path, struct stat *ub)
1590 * Get file status; this version follows links.
1593 stat(struct stat_args *uap)
1595 struct thread *td = curthread;
1596 struct nameidata nd;
1600 NDINIT(&nd, NAMEI_LOOKUP, CNP_FOLLOW | CNP_LOCKLEAF | CNP_NOOBJ,
1601 UIO_USERSPACE, uap->path, td);
1603 error = kern_stat(&nd, &st);
1606 error = copyout(&st, uap->ub, sizeof(*uap->ub));
1611 * lstat_args(char *path, struct stat *ub)
1613 * Get file status; this version does not follow links.
1616 lstat(struct lstat_args *uap)
1618 struct thread *td = curthread;
1619 struct nameidata nd;
1623 NDINIT(&nd, NAMEI_LOOKUP, CNP_LOCKLEAF | CNP_NOOBJ,
1624 UIO_USERSPACE, SCARG(uap, path), td);
1626 error = kern_stat(&nd, &st);
1629 error = copyout(&st, uap->ub, sizeof(*uap->ub));
1638 nsb->st_dev = sb->st_dev;
1639 nsb->st_ino = sb->st_ino;
1640 nsb->st_mode = sb->st_mode;
1641 nsb->st_nlink = sb->st_nlink;
1642 nsb->st_uid = sb->st_uid;
1643 nsb->st_gid = sb->st_gid;
1644 nsb->st_rdev = sb->st_rdev;
1645 nsb->st_atimespec = sb->st_atimespec;
1646 nsb->st_mtimespec = sb->st_mtimespec;
1647 nsb->st_ctimespec = sb->st_ctimespec;
1648 nsb->st_size = sb->st_size;
1649 nsb->st_blocks = sb->st_blocks;
1650 nsb->st_blksize = sb->st_blksize;
1651 nsb->st_flags = sb->st_flags;
1652 nsb->st_gen = sb->st_gen;
1653 nsb->st_qspare[0] = sb->st_qspare[0];
1654 nsb->st_qspare[1] = sb->st_qspare[1];
1658 * nstat_args(char *path, struct nstat *ub)
1662 nstat(struct nstat_args *uap)
1664 struct thread *td = curthread;
1668 struct nameidata nd;
1670 NDINIT(&nd, NAMEI_LOOKUP, CNP_FOLLOW | CNP_LOCKLEAF | CNP_NOOBJ,
1671 UIO_USERSPACE, SCARG(uap, path), td);
1672 if ((error = namei(&nd)) != 0)
1674 NDFREE(&nd, NDF_ONLY_PNBUF);
1675 error = vn_stat(nd.ni_vp, &sb, td);
1679 cvtnstat(&sb, &nsb);
1680 error = copyout((caddr_t)&nsb, (caddr_t)SCARG(uap, ub), sizeof (nsb));
1685 * lstat_args(char *path, struct stat *ub)
1687 * Get file status; this version does not follow links.
1691 nlstat(struct nlstat_args *uap)
1693 struct thread *td = curthread;
1698 struct nameidata nd;
1700 NDINIT(&nd, NAMEI_LOOKUP, CNP_LOCKLEAF | CNP_NOOBJ,
1701 UIO_USERSPACE, SCARG(uap, path), td);
1702 if ((error = namei(&nd)) != 0)
1705 NDFREE(&nd, NDF_ONLY_PNBUF);
1706 error = vn_stat(vp, &sb, td);
1710 cvtnstat(&sb, &nsb);
1711 error = copyout((caddr_t)&nsb, (caddr_t)SCARG(uap, ub), sizeof (nsb));
1716 * pathconf_Args(char *path, int name)
1718 * Get configurable pathname variables.
1722 pathconf(struct pathconf_args *uap)
1724 struct thread *td = curthread;
1726 struct nameidata nd;
1728 NDINIT(&nd, NAMEI_LOOKUP, CNP_FOLLOW | CNP_LOCKLEAF | CNP_NOOBJ,
1729 UIO_USERSPACE, SCARG(uap, path), td);
1730 if ((error = namei(&nd)) != 0)
1732 NDFREE(&nd, NDF_ONLY_PNBUF);
1733 error = VOP_PATHCONF(nd.ni_vp, SCARG(uap, name), uap->sysmsg_fds);
1740 * kern_readlink isn't properly split yet. There is a copyin burried
1741 * in VOP_READLINK().
1744 kern_readlink(struct nameidata *nd, char *buf, int count, int *res)
1746 struct thread *td = curthread;
1747 struct proc *p = td->td_proc;
1756 NDFREE(nd, NDF_ONLY_PNBUF);
1758 if (vp->v_type != VLNK)
1761 aiov.iov_base = buf;
1762 aiov.iov_len = count;
1763 auio.uio_iov = &aiov;
1764 auio.uio_iovcnt = 1;
1765 auio.uio_offset = 0;
1766 auio.uio_rw = UIO_READ;
1767 auio.uio_segflg = UIO_USERSPACE;
1769 auio.uio_resid = count;
1770 error = VOP_READLINK(vp, &auio, p->p_ucred);
1773 *res = count - auio.uio_resid;
1778 * readlink_args(char *path, char *buf, int count)
1780 * Return target name of a symbolic link.
1783 readlink(struct readlink_args *uap)
1785 struct thread *td = curthread;
1786 struct nameidata nd;
1789 NDINIT(&nd, NAMEI_LOOKUP, CNP_LOCKLEAF | CNP_NOOBJ, UIO_USERSPACE,
1792 error = kern_readlink(&nd, uap->buf, uap->count,
1793 &uap->sysmsg_result);
1799 setfflags(struct vnode *vp, int flags)
1801 struct thread *td = curthread;
1802 struct proc *p = td->td_proc;
1807 * Prevent non-root users from setting flags on devices. When
1808 * a device is reused, users can retain ownership of the device
1809 * if they are allowed to set flags and programs assume that
1810 * chown can't fail when done as root.
1812 if ((vp->v_type == VCHR || vp->v_type == VBLK) &&
1813 ((error = suser_cred(p->p_ucred, PRISON_ROOT)) != 0))
1816 VOP_LEASE(vp, td, p->p_ucred, LEASE_WRITE);
1817 vn_lock(vp, NULL, LK_EXCLUSIVE | LK_RETRY, td);
1819 vattr.va_flags = flags;
1820 error = VOP_SETATTR(vp, &vattr, p->p_ucred, td);
1821 VOP_UNLOCK(vp, NULL, 0, td);
1826 * chflags(char *path, int flags)
1828 * Change flags of a file given a path name.
1832 chflags(struct chflags_args *uap)
1834 struct thread *td = curthread;
1836 struct nameidata nd;
1838 NDINIT(&nd, NAMEI_LOOKUP, CNP_FOLLOW, UIO_USERSPACE,
1839 SCARG(uap, path), td);
1840 if ((error = namei(&nd)) != 0)
1842 NDFREE(&nd, NDF_ONLY_PNBUF);
1843 error = setfflags(nd.ni_vp, SCARG(uap, flags));
1849 * fchflags_args(int fd, int flags)
1851 * Change flags of a file given a file descriptor.
1855 fchflags(struct fchflags_args *uap)
1857 struct thread *td = curthread;
1858 struct proc *p = td->td_proc;
1862 if ((error = getvnode(p->p_fd, SCARG(uap, fd), &fp)) != 0)
1864 return setfflags((struct vnode *) fp->f_data, SCARG(uap, flags));
1868 setfmode(struct vnode *vp, int mode)
1870 struct thread *td = curthread;
1871 struct proc *p = td->td_proc;
1875 VOP_LEASE(vp, td, p->p_ucred, LEASE_WRITE);
1876 vn_lock(vp, NULL, LK_EXCLUSIVE | LK_RETRY, td);
1878 vattr.va_mode = mode & ALLPERMS;
1879 error = VOP_SETATTR(vp, &vattr, p->p_ucred, td);
1880 VOP_UNLOCK(vp, NULL, 0, td);
1885 kern_chmod(struct nameidata *nd, int mode)
1892 NDFREE(nd, NDF_ONLY_PNBUF);
1893 error = setfmode(nd->ni_vp, mode);
1899 * chmod_args(char *path, int mode)
1901 * Change mode of a file given path name.
1905 chmod(struct chmod_args *uap)
1907 struct thread *td = curthread;
1908 struct nameidata nd;
1911 NDINIT(&nd, NAMEI_LOOKUP, CNP_FOLLOW, UIO_USERSPACE, uap->path, td);
1913 error = kern_chmod(&nd, uap->mode);
1919 * lchmod_args(char *path, int mode)
1921 * Change mode of a file given path name (don't follow links.)
1925 lchmod(struct lchmod_args *uap)
1927 struct thread *td = curthread;
1929 struct nameidata nd;
1931 NDINIT(&nd, NAMEI_LOOKUP, 0, UIO_USERSPACE, SCARG(uap, path), td);
1932 if ((error = namei(&nd)) != 0)
1934 NDFREE(&nd, NDF_ONLY_PNBUF);
1935 error = setfmode(nd.ni_vp, SCARG(uap, mode));
1941 * fchmod_args(int fd, int mode)
1943 * Change mode of a file given a file descriptor.
1947 fchmod(struct fchmod_args *uap)
1949 struct thread *td = curthread;
1950 struct proc *p = td->td_proc;
1954 if ((error = getvnode(p->p_fd, SCARG(uap, fd), &fp)) != 0)
1956 return setfmode((struct vnode *)fp->f_data, SCARG(uap, mode));
1960 setfown(struct vnode *vp, uid_t uid, gid_t gid)
1962 struct thread *td = curthread;
1963 struct proc *p = td->td_proc;
1967 VOP_LEASE(vp, td, p->p_ucred, LEASE_WRITE);
1968 vn_lock(vp, NULL, LK_EXCLUSIVE | LK_RETRY, td);
1972 error = VOP_SETATTR(vp, &vattr, p->p_ucred, td);
1973 VOP_UNLOCK(vp, NULL, 0, td);
1978 kern_chown(struct nameidata *nd, int uid, int gid)
1985 NDFREE(nd, NDF_ONLY_PNBUF);
1986 error = setfown(nd->ni_vp, uid, gid);
1992 * chown(char *path, int uid, int gid)
1994 * Set ownership given a path name.
1997 chown(struct chown_args *uap)
1999 struct thread *td = curthread;
2000 struct nameidata nd;
2003 NDINIT(&nd, NAMEI_LOOKUP, CNP_FOLLOW, UIO_USERSPACE, uap->path, td);
2005 error = kern_chown(&nd, uap->uid, uap->gid);
2011 * lchown_args(char *path, int uid, int gid)
2013 * Set ownership given a path name, do not cross symlinks.
2016 lchown(struct lchown_args *uap)
2018 struct thread *td = curthread;
2020 struct nameidata nd;
2022 NDINIT(&nd, NAMEI_LOOKUP, 0, UIO_USERSPACE, uap->path, td);
2024 error = kern_chown(&nd, uap->uid, uap->gid);
2030 * fchown_args(int fd, int uid, int gid)
2032 * Set ownership given a file descriptor.
2036 fchown(struct fchown_args *uap)
2038 struct thread *td = curthread;
2039 struct proc *p = td->td_proc;
2043 if ((error = getvnode(p->p_fd, SCARG(uap, fd), &fp)) != 0)
2045 return setfown((struct vnode *)fp->f_data,
2046 SCARG(uap, uid), SCARG(uap, gid));
2050 getutimes(const struct timeval *tvp, struct timespec *tsp)
2052 struct timeval tv[2];
2056 TIMEVAL_TO_TIMESPEC(&tv[0], &tsp[0]);
2059 TIMEVAL_TO_TIMESPEC(&tvp[0], &tsp[0]);
2060 TIMEVAL_TO_TIMESPEC(&tvp[1], &tsp[1]);
2066 setutimes(struct vnode *vp, const struct timespec *ts, int nullflag)
2068 struct thread *td = curthread;
2069 struct proc *p = td->td_proc;
2073 VOP_LEASE(vp, td, p->p_ucred, LEASE_WRITE);
2074 vn_lock(vp, NULL, LK_EXCLUSIVE | LK_RETRY, td);
2076 vattr.va_atime = ts[0];
2077 vattr.va_mtime = ts[1];
2079 vattr.va_vaflags |= VA_UTIMES_NULL;
2080 error = VOP_SETATTR(vp, &vattr, p->p_ucred, td);
2081 VOP_UNLOCK(vp, NULL, 0, td);
2086 kern_utimes(struct nameidata *nd, struct timeval *tptr)
2088 struct timespec ts[2];
2091 error = getutimes(tptr, ts);
2097 NDFREE(nd, NDF_ONLY_PNBUF);
2098 error = setutimes(nd->ni_vp, ts, tptr == NULL);
2104 * utimes_args(char *path, struct timeval *tptr)
2106 * Set the access and modification times of a file.
2109 utimes(struct utimes_args *uap)
2111 struct thread *td = curthread;
2112 struct timeval tv[2];
2113 struct nameidata nd;
2117 error = copyin(uap->tptr, tv, sizeof(tv));
2121 NDINIT(&nd, NAMEI_LOOKUP, CNP_FOLLOW, UIO_USERSPACE, uap->path, td);
2123 error = kern_utimes(&nd, uap->tptr ? tv : NULL);
2129 * lutimes_args(char *path, struct timeval *tptr)
2131 * Set the access and modification times of a file.
2134 lutimes(struct lutimes_args *uap)
2136 struct thread *td = curthread;
2137 struct timeval tv[2];
2138 struct nameidata nd;
2142 error = copyin(uap->tptr, tv, sizeof(tv));
2146 NDINIT(&nd, NAMEI_LOOKUP, 0, UIO_USERSPACE, uap->path, td);
2148 error = kern_utimes(&nd, uap->tptr ? tv : NULL);
2154 kern_futimes(int fd, struct timeval *tptr)
2156 struct thread *td = curthread;
2157 struct proc *p = td->td_proc;
2158 struct timespec ts[2];
2162 error = getutimes(tptr, ts);
2165 error = getvnode(p->p_fd, fd, &fp);
2168 error = setutimes((struct vnode *)fp->f_data, ts, tptr == NULL);
2173 * futimes_args(int fd, struct timeval *tptr)
2175 * Set the access and modification times of a file.
2178 futimes(struct futimes_args *uap)
2180 struct timeval tv[2];
2184 error = copyin(uap->tptr, tv, sizeof(tv));
2189 error = kern_futimes(uap->fd, uap->tptr ? tv : NULL);
2195 kern_truncate(struct nameidata* nd, off_t length)
2197 struct thread *td = curthread;
2198 struct proc *p = td->td_proc;
2205 if ((error = namei(nd)) != 0)
2208 NDFREE(nd, NDF_ONLY_PNBUF);
2209 VOP_LEASE(vp, td, p->p_ucred, LEASE_WRITE);
2210 vn_lock(vp, NULL, LK_EXCLUSIVE | LK_RETRY, td);
2211 if (vp->v_type == VDIR)
2213 else if ((error = vn_writechk(vp)) == 0 &&
2214 (error = VOP_ACCESS(vp, VWRITE, p->p_ucred, td)) == 0) {
2216 vattr.va_size = length;
2217 error = VOP_SETATTR(vp, &vattr, p->p_ucred, td);
2224 * truncate(char *path, int pad, off_t length)
2226 * Truncate a file given its path name.
2229 truncate(struct truncate_args *uap)
2231 struct thread *td = curthread;
2232 struct nameidata nd;
2235 NDINIT(&nd, NAMEI_LOOKUP, CNP_FOLLOW, UIO_USERSPACE, uap->path, td);
2237 error = kern_truncate(&nd, uap->length);
2243 kern_ftruncate(int fd, off_t length)
2245 struct thread *td = curthread;
2246 struct proc *p = td->td_proc;
2254 if ((error = getvnode(p->p_fd, fd, &fp)) != 0)
2256 if ((fp->f_flag & FWRITE) == 0)
2258 vp = (struct vnode *)fp->f_data;
2259 VOP_LEASE(vp, td, p->p_ucred, LEASE_WRITE);
2260 vn_lock(vp, NULL, LK_EXCLUSIVE | LK_RETRY, td);
2261 if (vp->v_type == VDIR)
2263 else if ((error = vn_writechk(vp)) == 0) {
2265 vattr.va_size = length;
2266 error = VOP_SETATTR(vp, &vattr, fp->f_cred, td);
2268 VOP_UNLOCK(vp, NULL, 0, td);
2273 * ftruncate_args(int fd, int pad, off_t length)
2275 * Truncate a file given a file descriptor.
2278 ftruncate(struct ftruncate_args *uap)
2282 error = kern_ftruncate(uap->fd, uap->length);
2290 * Sync an open file.
2294 fsync(struct fsync_args *uap)
2296 struct thread *td = curthread;
2297 struct proc *p = td->td_proc;
2303 if ((error = getvnode(p->p_fd, SCARG(uap, fd), &fp)) != 0)
2305 vp = (struct vnode *)fp->f_data;
2306 vn_lock(vp, NULL, LK_EXCLUSIVE | LK_RETRY, td);
2307 if (VOP_GETVOBJECT(vp, &obj) == 0)
2308 vm_object_page_clean(obj, 0, 0, 0);
2309 if ((error = VOP_FSYNC(vp, MNT_WAIT, td)) == 0 &&
2310 vp->v_mount && (vp->v_mount->mnt_flag & MNT_SOFTDEP) &&
2312 error = (*bioops.io_fsync)(vp);
2313 VOP_UNLOCK(vp, NULL, 0, td);
2318 kern_rename(struct nameidata *fromnd, struct nameidata *tond)
2320 struct thread *td = curthread;
2321 struct proc *p = td->td_proc;
2322 struct vnode *tvp, *fvp, *tdvp;
2326 error = namei(fromnd);
2329 fvp = fromnd->ni_vp;
2330 if (fromnd->ni_vp->v_type == VDIR)
2331 tond->ni_cnd.cn_flags |= CNP_WILLBEDIR;
2332 error = namei(tond);
2334 /* Translate error code for rename("dir1", "dir2/."). */
2335 if (error == EISDIR && fvp->v_type == VDIR)
2337 NDFREE(fromnd, NDF_ONLY_PNBUF);
2338 vrele(fromnd->ni_dvp);
2342 tdvp = tond->ni_dvp;
2345 if (fvp->v_type == VDIR && tvp->v_type != VDIR) {
2348 } else if (fvp->v_type != VDIR && tvp->v_type == VDIR) {
2356 * If the source is the same as the destination (that is, if they
2357 * are links to the same vnode), then there is nothing to do.
2363 VOP_LEASE(tdvp, td, p->p_ucred, LEASE_WRITE);
2364 if (fromnd->ni_dvp != tdvp) {
2365 VOP_LEASE(fromnd->ni_dvp, td, p->p_ucred, LEASE_WRITE);
2368 VOP_LEASE(tvp, td, p->p_ucred, LEASE_WRITE);
2370 error = VOP_RENAME(fromnd->ni_dvp, NCPNULL, fromnd->ni_vp,
2371 &fromnd->ni_cnd, tond->ni_dvp, NCPNULL, tond->ni_vp,
2373 NDFREE(fromnd, NDF_ONLY_PNBUF);
2374 NDFREE(tond, NDF_ONLY_PNBUF);
2376 NDFREE(fromnd, NDF_ONLY_PNBUF);
2377 NDFREE(tond, NDF_ONLY_PNBUF);
2384 vrele(fromnd->ni_dvp);
2387 vrele(tond->ni_startdir);
2388 ASSERT_VOP_UNLOCKED(fromnd->ni_dvp, "rename");
2389 ASSERT_VOP_UNLOCKED(fromnd->ni_vp, "rename");
2390 ASSERT_VOP_UNLOCKED(tond->ni_dvp, "rename");
2391 ASSERT_VOP_UNLOCKED(tond->ni_vp, "rename");
2393 if (fromnd->ni_startdir)
2394 vrele(fromnd->ni_startdir);
2401 * rename_args(char *from, char *to)
2403 * Rename files. Source and destination must either both be directories,
2404 * or both not be directories. If target is a directory, it must be empty.
2407 rename(struct rename_args *uap)
2409 struct thread *td = curthread;
2410 struct nameidata fromnd, tond;
2413 NDINIT(&fromnd, NAMEI_DELETE, CNP_WANTPARENT | CNP_SAVESTART,
2414 UIO_USERSPACE, uap->from, td);
2415 NDINIT(&tond, NAMEI_RENAME,
2416 CNP_LOCKPARENT | CNP_LOCKLEAF | CNP_NOCACHE |
2417 CNP_SAVESTART | CNP_NOOBJ,
2418 UIO_USERSPACE, uap->to, td);
2420 error = kern_rename(&fromnd, &tond);
2426 kern_mkdir(struct nameidata *nd, int mode)
2428 struct thread *td = curthread;
2429 struct proc *p = td->td_proc;
2435 nd->ni_cnd.cn_flags |= CNP_WILLBEDIR;
2441 NDFREE(nd, NDF_ONLY_PNBUF);
2442 if (nd->ni_dvp == vp)
2450 vattr.va_type = VDIR;
2451 vattr.va_mode = (mode & ACCESSPERMS) &~ p->p_fd->fd_cmask;
2452 VOP_LEASE(nd->ni_dvp, td, p->p_ucred, LEASE_WRITE);
2453 error = VOP_MKDIR(nd->ni_dvp, NCPNULL, &nd->ni_vp, &nd->ni_cnd,
2455 NDFREE(nd, NDF_ONLY_PNBUF);
2459 ASSERT_VOP_UNLOCKED(nd->ni_dvp, "mkdir");
2460 ASSERT_VOP_UNLOCKED(nd->ni_vp, "mkdir");
2465 * mkdir_args(char *path, int mode)
2467 * Make a directory file.
2471 mkdir(struct mkdir_args *uap)
2473 struct thread *td = curthread;
2474 struct nameidata nd;
2477 NDINIT(&nd, NAMEI_CREATE, CNP_LOCKPARENT, UIO_USERSPACE, uap->path,
2480 error = kern_mkdir(&nd, uap->mode);
2486 kern_rmdir(struct nameidata *nd)
2488 struct thread *td = curthread;
2489 struct proc *p = td->td_proc;
2498 if (vp->v_type != VDIR) {
2503 * No rmdir "." please.
2505 if (nd->ni_dvp == vp) {
2510 * The root of a mounted filesystem cannot be deleted.
2512 if (vp->v_flag & VROOT)
2515 VOP_LEASE(nd->ni_dvp, td, p->p_ucred, LEASE_WRITE);
2516 VOP_LEASE(vp, td, p->p_ucred, LEASE_WRITE);
2517 error = VOP_RMDIR(nd->ni_dvp, NCPNULL, nd->ni_vp,
2521 NDFREE(nd, NDF_ONLY_PNBUF);
2522 if (nd->ni_dvp == vp)
2528 ASSERT_VOP_UNLOCKED(nd->ni_dvp, "rmdir");
2529 ASSERT_VOP_UNLOCKED(nd->ni_vp, "rmdir");
2534 * rmdir_args(char *path)
2536 * Remove a directory file.
2540 rmdir(struct rmdir_args *uap)
2542 struct thread *td = curthread;
2543 struct nameidata nd;
2546 NDINIT(&nd, NAMEI_DELETE, CNP_LOCKPARENT | CNP_LOCKLEAF,
2547 UIO_USERSPACE, uap->path, td);
2549 error = kern_rmdir(&nd);
2555 kern_getdirentries(int fd, char *buf, u_int count, long *basep, int *res)
2557 struct thread *td = curthread;
2558 struct proc *p = td->td_proc;
2566 if ((error = getvnode(p->p_fd, fd, &fp)) != 0)
2568 if ((fp->f_flag & FREAD) == 0)
2570 vp = (struct vnode *)fp->f_data;
2572 if (vp->v_type != VDIR)
2574 aiov.iov_base = buf;
2575 aiov.iov_len = count;
2576 auio.uio_iov = &aiov;
2577 auio.uio_iovcnt = 1;
2578 auio.uio_rw = UIO_READ;
2579 auio.uio_segflg = UIO_USERSPACE;
2581 auio.uio_resid = count;
2582 /* vn_lock(vp, NULL, LK_SHARED | LK_RETRY, td); */
2583 vn_lock(vp, NULL, LK_EXCLUSIVE | LK_RETRY, td);
2584 loff = auio.uio_offset = fp->f_offset;
2585 error = VOP_READDIR(vp, &auio, fp->f_cred, &eofflag, NULL, NULL);
2586 fp->f_offset = auio.uio_offset;
2587 VOP_UNLOCK(vp, NULL, 0, td);
2590 if (count == auio.uio_resid) {
2591 if (union_dircheckp) {
2592 error = union_dircheckp(td, &vp, fp);
2598 if ((vp->v_flag & VROOT) &&
2599 (vp->v_mount->mnt_flag & MNT_UNION)) {
2600 struct vnode *tvp = vp;
2601 vp = vp->v_mount->mnt_vnodecovered;
2603 fp->f_data = (caddr_t) vp;
2612 *res = count - auio.uio_resid;
2617 * getdirentries_args(int fd, char *buf, u_int conut, long *basep)
2619 * Read a block of directory entries in a file system independent format.
2622 getdirentries(struct getdirentries_args *uap)
2627 error = kern_getdirentries(uap->fd, uap->buf, uap->count, &base,
2628 &uap->sysmsg_result);
2631 error = copyout(&base, uap->basep, sizeof(*uap->basep));
2636 * getdents_args(int fd, char *buf, size_t count)
2639 getdents(struct getdents_args *uap)
2643 error = kern_getdirentries(uap->fd, uap->buf, uap->count, NULL,
2644 &uap->sysmsg_result);
2650 * umask(int newmask)
2652 * Set the mode mask for creation of filesystem nodes.
2657 umask(struct umask_args *uap)
2659 struct thread *td = curthread;
2660 struct proc *p = td->td_proc;
2661 struct filedesc *fdp;
2664 uap->sysmsg_result = fdp->fd_cmask;
2665 fdp->fd_cmask = SCARG(uap, newmask) & ALLPERMS;
2670 * revoke(char *path)
2672 * Void all references to file by ripping underlying filesystem
2677 revoke(struct revoke_args *uap)
2679 struct thread *td = curthread;
2680 struct proc *p = td->td_proc;
2684 struct nameidata nd;
2686 NDINIT(&nd, NAMEI_LOOKUP, CNP_FOLLOW, UIO_USERSPACE, SCARG(uap, path), td);
2687 if ((error = namei(&nd)) != 0)
2690 NDFREE(&nd, NDF_ONLY_PNBUF);
2691 if (vp->v_type != VCHR && vp->v_type != VBLK) {
2695 if ((error = VOP_GETATTR(vp, &vattr, td)) != 0)
2697 if (p->p_ucred->cr_uid != vattr.va_uid &&
2698 (error = suser_cred(p->p_ucred, PRISON_ROOT)))
2701 VOP_REVOKE(vp, REVOKEALL);
2708 * Convert a user file descriptor to a kernel file entry.
2711 getvnode(struct filedesc *fdp, int fd, struct file **fpp)
2715 if ((u_int)fd >= fdp->fd_nfiles ||
2716 (fp = fdp->fd_ofiles[fd]) == NULL)
2718 if (fp->f_type != DTYPE_VNODE && fp->f_type != DTYPE_FIFO)
2724 * getfh_args(char *fname, fhandle_t *fhp)
2726 * Get (NFS) file handle
2729 getfh(struct getfh_args *uap)
2731 struct thread *td = curthread;
2732 struct nameidata nd;
2738 * Must be super user
2743 NDINIT(&nd, NAMEI_LOOKUP, CNP_FOLLOW | CNP_LOCKLEAF, UIO_USERSPACE, uap->fname, td);
2747 NDFREE(&nd, NDF_ONLY_PNBUF);
2749 bzero(&fh, sizeof(fh));
2750 fh.fh_fsid = vp->v_mount->mnt_stat.f_fsid;
2751 error = VFS_VPTOFH(vp, &fh.fh_fid);
2755 error = copyout(&fh, uap->fhp, sizeof (fh));
2760 * fhopen_args(const struct fhandle *u_fhp, int flags)
2762 * syscall for the rpc.lockd to use to translate a NFS file handle into
2763 * an open descriptor.
2765 * warning: do not remove the suser() call or this becomes one giant
2769 fhopen(struct fhopen_args *uap)
2771 struct thread *td = curthread;
2772 struct proc *p = td->td_proc;
2777 struct vattr *vap = &vat;
2780 struct filedesc *fdp = p->p_fd;
2781 int fmode, mode, error, type;
2786 * Must be super user
2792 fmode = FFLAGS(SCARG(uap, flags));
2793 /* why not allow a non-read/write open for our lockd? */
2794 if (((fmode & (FREAD | FWRITE)) == 0) || (fmode & O_CREAT))
2796 error = copyin(SCARG(uap,u_fhp), &fhp, sizeof(fhp));
2799 /* find the mount point */
2800 mp = vfs_getvfs(&fhp.fh_fsid);
2803 /* now give me my vnode, it gets returned to me locked */
2804 error = VFS_FHTOVP(mp, &fhp.fh_fid, &vp);
2808 * from now on we have to make sure not
2809 * to forget about the vnode
2810 * any error that causes an abort must vput(vp)
2811 * just set error = err and 'goto bad;'.
2817 if (vp->v_type == VLNK) {
2821 if (vp->v_type == VSOCK) {
2826 if (fmode & (FWRITE | O_TRUNC)) {
2827 if (vp->v_type == VDIR) {
2831 error = vn_writechk(vp);
2839 error = VOP_ACCESS(vp, mode, p->p_ucred, td);
2843 if (fmode & O_TRUNC) {
2844 VOP_UNLOCK(vp, NULL, 0, td); /* XXX */
2845 VOP_LEASE(vp, td, p->p_ucred, LEASE_WRITE);
2846 vn_lock(vp, NULL, LK_EXCLUSIVE | LK_RETRY, td); /* XXX */
2849 error = VOP_SETATTR(vp, vap, p->p_ucred, td);
2853 error = VOP_OPEN(vp, fmode, p->p_ucred, td);
2857 * Make sure that a VM object is created for VMIO support.
2859 if (vn_canvmio(vp) == TRUE) {
2860 if ((error = vfs_object_create(vp, td)) != 0)
2867 * end of vn_open code
2870 if ((error = falloc(p, &nfp, &indx)) != 0) {
2878 * hold an extra reference to avoid having fp ripped out
2879 * from under us while we block in the lock op.
2882 nfp->f_data = (caddr_t)vp;
2883 nfp->f_flag = fmode & FMASK;
2884 nfp->f_ops = &vnops;
2885 nfp->f_type = DTYPE_VNODE;
2886 if (fmode & (O_EXLOCK | O_SHLOCK)) {
2887 lf.l_whence = SEEK_SET;
2890 if (fmode & O_EXLOCK)
2891 lf.l_type = F_WRLCK;
2893 lf.l_type = F_RDLCK;
2895 if ((fmode & FNONBLOCK) == 0)
2897 VOP_UNLOCK(vp, NULL, 0, td);
2898 if ((error = VOP_ADVLOCK(vp, (caddr_t)fp, F_SETLK, &lf, type)) != 0) {
2900 * lock request failed. Normally close the descriptor
2901 * but handle the case where someone might have dup()d
2902 * or close()d it when we weren't looking.
2904 if (fdp->fd_ofiles[indx] == fp) {
2905 fdp->fd_ofiles[indx] = NULL;
2910 * release our private reference.
2915 vn_lock(vp, NULL, LK_EXCLUSIVE | LK_RETRY, td);
2916 fp->f_flag |= FHASLOCK;
2918 if ((vp->v_type == VREG) && (VOP_GETVOBJECT(vp, NULL) != 0))
2919 vfs_object_create(vp, td);
2921 VOP_UNLOCK(vp, NULL, 0, td);
2923 uap->sysmsg_result = indx;
2932 * fhstat_args(struct fhandle *u_fhp, struct stat *sb)
2935 fhstat(struct fhstat_args *uap)
2937 struct thread *td = curthread;
2945 * Must be super user
2951 error = copyin(SCARG(uap, u_fhp), &fh, sizeof(fhandle_t));
2955 if ((mp = vfs_getvfs(&fh.fh_fsid)) == NULL)
2957 if ((error = VFS_FHTOVP(mp, &fh.fh_fid, &vp)))
2959 error = vn_stat(vp, &sb, td);
2963 error = copyout(&sb, SCARG(uap, sb), sizeof(sb));
2968 * fhstatfs_args(struct fhandle *u_fhp, struct statfs *buf)
2971 fhstatfs(struct fhstatfs_args *uap)
2973 struct thread *td = curthread;
2982 * Must be super user
2984 if ((error = suser(td)))
2987 if ((error = copyin(SCARG(uap, u_fhp), &fh, sizeof(fhandle_t))) != 0)
2990 if ((mp = vfs_getvfs(&fh.fh_fsid)) == NULL)
2992 if ((error = VFS_FHTOVP(mp, &fh.fh_fid, &vp)))
2997 if ((error = VFS_STATFS(mp, sp, td)) != 0)
2999 sp->f_flags = mp->mnt_flag & MNT_VISFLAGMASK;
3001 bcopy((caddr_t)sp, (caddr_t)&sb, sizeof(sb));
3002 sb.f_fsid.val[0] = sb.f_fsid.val[1] = 0;
3005 return (copyout(sp, SCARG(uap, buf), sizeof(*sp)));
3009 * Syscall to push extended attribute configuration information into the
3010 * VFS. Accepts a path, which it converts to a mountpoint, as well as
3011 * a command (int cmd), and attribute name and misc data. For now, the
3012 * attribute name is left in userspace for consumption by the VFS_op.
3013 * It will probably be changed to be copied into sysspace by the
3014 * syscall in the future, once issues with various consumers of the
3015 * attribute code have raised their hands.
3017 * Currently this is used only by UFS Extended Attributes.
3020 extattrctl(struct extattrctl_args *uap)
3022 struct thread *td = curthread;
3023 struct nameidata nd;
3027 NDINIT(&nd, NAMEI_LOOKUP, CNP_FOLLOW, UIO_USERSPACE, SCARG(uap, path), td);
3028 if ((error = namei(&nd)) != 0)
3030 mp = nd.ni_vp->v_mount;
3032 return (VFS_EXTATTRCTL(mp, SCARG(uap, cmd), SCARG(uap, attrname),
3033 SCARG(uap, arg), td));
3037 * Syscall to set a named extended attribute on a file or directory.
3038 * Accepts attribute name, and a uio structure pointing to the data to set.
3039 * The uio is consumed in the style of writev(). The real work happens
3040 * in VOP_SETEXTATTR().
3043 extattr_set_file(struct extattr_set_file_args *uap)
3045 struct thread *td = curthread;
3046 struct proc *p = td->td_proc;
3047 struct nameidata nd;
3049 struct iovec *iov, *needfree = NULL, aiov[UIO_SMALLIOV];
3050 char attrname[EXTATTR_MAXNAMELEN];
3054 error = copyin(SCARG(uap, attrname), attrname, EXTATTR_MAXNAMELEN);
3057 NDINIT(&nd, NAMEI_LOOKUP, CNP_FOLLOW | CNP_LOCKLEAF, UIO_USERSPACE,
3058 SCARG(uap, path), td);
3059 if ((error = namei(&nd)) != 0)
3061 iovlen = uap->iovcnt * sizeof(struct iovec);
3062 if (uap->iovcnt > UIO_SMALLIOV) {
3063 if (uap->iovcnt > UIO_MAXIOV) {
3067 MALLOC(iov, struct iovec *, iovlen, M_IOV, M_WAITOK);
3072 auio.uio_iovcnt = uap->iovcnt;
3073 auio.uio_rw = UIO_WRITE;
3074 auio.uio_segflg = UIO_USERSPACE;
3076 auio.uio_offset = 0;
3077 if ((error = copyin((caddr_t)uap->iovp, (caddr_t)iov, iovlen)))
3080 for (i = 0; i < uap->iovcnt; i++) {
3081 if (iov->iov_len > INT_MAX - auio.uio_resid) {
3085 auio.uio_resid += iov->iov_len;
3088 cnt = auio.uio_resid;
3089 error = VOP_SETEXTATTR(nd.ni_vp, attrname, &auio, p->p_ucred, td);
3090 cnt -= auio.uio_resid;
3091 uap->sysmsg_result = cnt;
3094 FREE(needfree, M_IOV);
3100 * Syscall to get a named extended attribute on a file or directory.
3101 * Accepts attribute name, and a uio structure pointing to a buffer for the
3102 * data. The uio is consumed in the style of readv(). The real work
3103 * happens in VOP_GETEXTATTR();
3106 extattr_get_file(struct extattr_get_file_args *uap)
3108 struct thread *td = curthread;
3109 struct proc *p = td->td_proc;
3110 struct nameidata nd;
3112 struct iovec *iov, *needfree, aiov[UIO_SMALLIOV];
3113 char attrname[EXTATTR_MAXNAMELEN];
3117 error = copyin(SCARG(uap, attrname), attrname, EXTATTR_MAXNAMELEN);
3120 NDINIT(&nd, NAMEI_LOOKUP, CNP_FOLLOW | CNP_LOCKLEAF, UIO_USERSPACE,
3121 SCARG(uap, path), td);
3122 if ((error = namei(&nd)) != 0)
3124 iovlen = uap->iovcnt * sizeof (struct iovec);
3125 if (uap->iovcnt > UIO_SMALLIOV) {
3126 if (uap->iovcnt > UIO_MAXIOV) {
3130 MALLOC(iov, struct iovec *, iovlen, M_IOV, M_WAITOK);
3137 auio.uio_iovcnt = uap->iovcnt;
3138 auio.uio_rw = UIO_READ;
3139 auio.uio_segflg = UIO_USERSPACE;
3141 auio.uio_offset = 0;
3142 if ((error = copyin((caddr_t)uap->iovp, (caddr_t)iov, iovlen)))
3145 for (i = 0; i < uap->iovcnt; i++) {
3146 if (iov->iov_len > INT_MAX - auio.uio_resid) {
3150 auio.uio_resid += iov->iov_len;
3153 cnt = auio.uio_resid;
3154 error = VOP_GETEXTATTR(nd.ni_vp, attrname, &auio, p->p_ucred, td);
3155 cnt -= auio.uio_resid;
3156 uap->sysmsg_result = cnt;
3159 FREE(needfree, M_IOV);
3165 * Syscall to delete a named extended attribute from a file or directory.
3166 * Accepts attribute name. The real work happens in VOP_SETEXTATTR().
3169 extattr_delete_file(struct extattr_delete_file_args *uap)
3171 struct thread *td = curthread;
3172 struct proc *p = td->td_proc;
3173 struct nameidata nd;
3174 char attrname[EXTATTR_MAXNAMELEN];
3177 error = copyin(SCARG(uap, attrname), attrname, EXTATTR_MAXNAMELEN);
3180 NDINIT(&nd, NAMEI_LOOKUP, CNP_FOLLOW | CNP_LOCKLEAF, UIO_USERSPACE,
3181 SCARG(uap, path), td);
3182 if ((error = namei(&nd)) != 0)
3184 error = VOP_SETEXTATTR(nd.ni_vp, attrname, NULL, p->p_ucred, td);