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.28 2003/11/14 19:31:22 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];
112 if (usermount == 0 && (error = suser(td)))
115 * Do not allow NFS export by non-root users.
117 if (SCARG(uap, flags) & MNT_EXPORTED) {
123 * Silently enforce MNT_NOSUID and MNT_NODEV for non-root users
126 SCARG(uap, flags) |= MNT_NOSUID | MNT_NODEV;
128 * Get vnode to be covered
130 NDINIT(&nd, NAMEI_LOOKUP, CNP_FOLLOW | CNP_LOCKLEAF, UIO_USERSPACE,
131 SCARG(uap, path), td);
132 if ((error = namei(&nd)) != 0)
134 NDFREE(&nd, NDF_ONLY_PNBUF);
136 if (SCARG(uap, flags) & MNT_UPDATE) {
137 if ((vp->v_flag & VROOT) == 0) {
143 flag2 = mp->mnt_kern_flag;
145 * We only allow the filesystem to be reloaded if it
146 * is currently mounted read-only.
148 if ((SCARG(uap, flags) & MNT_RELOAD) &&
149 ((mp->mnt_flag & MNT_RDONLY) == 0)) {
151 return (EOPNOTSUPP); /* Needs translation */
154 * Only root, or the user that did the original mount is
155 * permitted to update it.
157 if (mp->mnt_stat.f_owner != p->p_ucred->cr_uid &&
158 (error = suser(td))) {
162 if (vfs_busy(mp, LK_NOWAIT, 0, td)) {
166 lwkt_gettoken(&vp->v_interlock);
167 if ((vp->v_flag & VMOUNT) != 0 ||
168 vp->v_mountedhere != NULL) {
169 lwkt_reltoken(&vp->v_interlock);
174 vp->v_flag |= VMOUNT;
175 lwkt_reltoken(&vp->v_interlock);
177 SCARG(uap, flags) & (MNT_RELOAD | MNT_FORCE | MNT_UPDATE);
178 VOP_UNLOCK(vp, 0, td);
182 * If the user is not root, ensure that they own the directory
183 * onto which we are attempting to mount.
185 if ((error = VOP_GETATTR(vp, &va, td)) ||
186 (va.va_uid != p->p_ucred->cr_uid &&
187 (error = suser(td)))) {
191 if ((error = vinvalbuf(vp, V_SAVE, td, 0, 0)) != 0) {
195 if (vp->v_type != VDIR) {
199 if ((error = copyinstr(SCARG(uap, type), fstypename, MFSNAMELEN, NULL)) != 0) {
203 for (vfsp = vfsconf; vfsp; vfsp = vfsp->vfc_next)
204 if (!strcmp(vfsp->vfc_name, fstypename))
209 /* Only load modules for root (very important!) */
210 if ((error = suser(td)) != 0) {
214 error = linker_load_file(fstypename, &lf);
215 if (error || lf == NULL) {
222 /* lookup again, see if the VFS was loaded */
223 for (vfsp = vfsconf; vfsp; vfsp = vfsp->vfc_next)
224 if (!strcmp(vfsp->vfc_name, fstypename))
228 linker_file_unload(lf);
233 lwkt_gettoken(&vp->v_interlock);
234 if ((vp->v_flag & VMOUNT) != 0 ||
235 vp->v_mountedhere != NULL) {
236 lwkt_reltoken(&vp->v_interlock);
240 vp->v_flag |= VMOUNT;
241 lwkt_reltoken(&vp->v_interlock);
244 * Allocate and initialize the filesystem.
246 mp = malloc(sizeof(struct mount), M_MOUNT, M_WAITOK);
247 bzero((char *)mp, (u_long)sizeof(struct mount));
248 TAILQ_INIT(&mp->mnt_nvnodelist);
249 TAILQ_INIT(&mp->mnt_reservedvnlist);
250 mp->mnt_nvnodelistsize = 0;
251 lockinit(&mp->mnt_lock, 0, "vfslock", 0, LK_NOPAUSE);
252 (void)vfs_busy(mp, LK_NOWAIT, 0, td);
253 mp->mnt_op = vfsp->vfc_vfsops;
255 vfsp->vfc_refcount++;
256 mp->mnt_stat.f_type = vfsp->vfc_typenum;
257 mp->mnt_flag |= vfsp->vfc_flags & MNT_VISFLAGMASK;
258 strncpy(mp->mnt_stat.f_fstypename, vfsp->vfc_name, MFSNAMELEN);
259 mp->mnt_vnodecovered = vp;
260 mp->mnt_stat.f_owner = p->p_ucred->cr_uid;
261 mp->mnt_iosize_max = DFLTPHYS;
262 VOP_UNLOCK(vp, 0, td);
265 * Set the mount level flags.
267 if (SCARG(uap, flags) & MNT_RDONLY)
268 mp->mnt_flag |= MNT_RDONLY;
269 else if (mp->mnt_flag & MNT_RDONLY)
270 mp->mnt_kern_flag |= MNTK_WANTRDWR;
271 mp->mnt_flag &=~ (MNT_NOSUID | MNT_NOEXEC | MNT_NODEV |
272 MNT_SYNCHRONOUS | MNT_UNION | MNT_ASYNC | MNT_NOATIME |
273 MNT_NOSYMFOLLOW | MNT_IGNORE |
274 MNT_NOCLUSTERR | MNT_NOCLUSTERW | MNT_SUIDDIR);
275 mp->mnt_flag |= SCARG(uap, flags) & (MNT_NOSUID | MNT_NOEXEC |
276 MNT_NODEV | MNT_SYNCHRONOUS | MNT_UNION | MNT_ASYNC | MNT_FORCE |
277 MNT_NOSYMFOLLOW | MNT_IGNORE |
278 MNT_NOATIME | MNT_NOCLUSTERR | MNT_NOCLUSTERW | MNT_SUIDDIR);
280 * Mount the filesystem.
281 * XXX The final recipients of VFS_MOUNT just overwrite the ndp they
282 * get. No freeing of cn_pnbuf.
284 error = VFS_MOUNT(mp, SCARG(uap, path), SCARG(uap, data), &nd, td);
285 if (mp->mnt_flag & MNT_UPDATE) {
286 if (mp->mnt_kern_flag & MNTK_WANTRDWR)
287 mp->mnt_flag &= ~MNT_RDONLY;
288 mp->mnt_flag &=~ (MNT_UPDATE | MNT_RELOAD | MNT_FORCE);
289 mp->mnt_kern_flag &=~ MNTK_WANTRDWR;
292 mp->mnt_kern_flag = flag2;
294 if ((mp->mnt_flag & MNT_RDONLY) == 0) {
295 if (mp->mnt_syncer == NULL)
296 error = vfs_allocate_syncvnode(mp);
298 if (mp->mnt_syncer != NULL)
299 vrele(mp->mnt_syncer);
300 mp->mnt_syncer = NULL;
303 lwkt_gettoken(&vp->v_interlock);
304 vp->v_flag &= ~VMOUNT;
305 lwkt_reltoken(&vp->v_interlock);
309 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY, td);
311 * Put the new filesystem on the mount list after root.
315 lwkt_gettoken(&vp->v_interlock);
316 vp->v_flag &= ~VMOUNT;
317 vp->v_mountedhere = mp;
318 lwkt_reltoken(&vp->v_interlock);
319 lwkt_gettoken(&mountlist_token);
320 TAILQ_INSERT_TAIL(&mountlist, mp, mnt_list);
321 lwkt_reltoken(&mountlist_token);
323 VOP_UNLOCK(vp, 0, td);
324 if ((mp->mnt_flag & MNT_RDONLY) == 0)
325 error = vfs_allocate_syncvnode(mp);
327 if ((error = VFS_START(mp, 0, td)) != 0)
330 lwkt_gettoken(&vp->v_interlock);
331 vp->v_flag &= ~VMOUNT;
332 lwkt_reltoken(&vp->v_interlock);
333 mp->mnt_vfc->vfc_refcount--;
335 free((caddr_t)mp, M_MOUNT);
342 * Scan all active processes to see if any of them have a current
343 * or root directory onto which the new filesystem has just been
344 * mounted. If so, replace them with the new mount point.
347 checkdirs(struct vnode *olddp)
349 struct filedesc *fdp;
353 if (olddp->v_usecount == 1)
355 if (VFS_ROOT(olddp->v_mountedhere, &newdp))
356 panic("mount: lost mount");
357 FOREACH_PROC_IN_SYSTEM(p) {
359 if (fdp->fd_cdir == olddp) {
362 fdp->fd_cdir = newdp;
364 if (fdp->fd_rdir == olddp) {
367 fdp->fd_rdir = newdp;
370 if (rootvnode == olddp) {
374 vfs_cache_setroot(rootvnode);
380 * Unmount a file system.
382 * Note: unmount takes a path to the vnode mounted on as argument,
383 * not special file (as before).
386 * umount_args(char *path, int flags)
390 unmount(struct unmount_args *uap)
392 struct thread *td = curthread;
393 struct proc *p = td->td_proc;
400 NDINIT(&nd, NAMEI_LOOKUP, CNP_FOLLOW | CNP_LOCKLEAF, UIO_USERSPACE,
401 SCARG(uap, path), td);
402 if ((error = namei(&nd)) != 0)
405 NDFREE(&nd, NDF_ONLY_PNBUF);
409 * Only root, or the user that did the original mount is
410 * permitted to unmount this filesystem.
412 if ((mp->mnt_stat.f_owner != p->p_ucred->cr_uid) &&
413 (error = suser(td))) {
419 * Don't allow unmounting the root file system.
421 if (mp->mnt_flag & MNT_ROOTFS) {
427 * Must be the root of the filesystem
429 if ((vp->v_flag & VROOT) == 0) {
434 return (dounmount(mp, SCARG(uap, flags), td));
438 * Do the actual file system unmount.
441 dounmount(struct mount *mp, int flags, struct thread *td)
443 struct vnode *coveredvp;
447 lwkt_gettoken(&mountlist_token);
448 if (mp->mnt_kern_flag & MNTK_UNMOUNT) {
449 lwkt_reltoken(&mountlist_token);
452 mp->mnt_kern_flag |= MNTK_UNMOUNT;
453 /* Allow filesystems to detect that a forced unmount is in progress. */
454 if (flags & MNT_FORCE)
455 mp->mnt_kern_flag |= MNTK_UNMOUNTF;
456 error = lockmgr(&mp->mnt_lock, LK_DRAIN | LK_INTERLOCK |
457 ((flags & MNT_FORCE) ? 0 : LK_NOWAIT), &mountlist_token, td);
459 mp->mnt_kern_flag &= ~(MNTK_UNMOUNT | MNTK_UNMOUNTF);
460 if (mp->mnt_kern_flag & MNTK_MWAIT)
465 if (mp->mnt_flag & MNT_EXPUBLIC)
466 vfs_setpublicfs(NULL, NULL, NULL);
468 vfs_msync(mp, MNT_WAIT);
469 async_flag = mp->mnt_flag & MNT_ASYNC;
470 mp->mnt_flag &=~ MNT_ASYNC;
471 cache_purgevfs(mp); /* remove cache entries for this file sys */
472 if (mp->mnt_syncer != NULL)
473 vrele(mp->mnt_syncer);
474 if (((mp->mnt_flag & MNT_RDONLY) ||
475 (error = VFS_SYNC(mp, MNT_WAIT, td)) == 0) ||
477 error = VFS_UNMOUNT(mp, flags, td);
478 lwkt_gettoken(&mountlist_token);
480 if ((mp->mnt_flag & MNT_RDONLY) == 0 && mp->mnt_syncer == NULL)
481 (void) vfs_allocate_syncvnode(mp);
482 mp->mnt_kern_flag &= ~(MNTK_UNMOUNT | MNTK_UNMOUNTF);
483 mp->mnt_flag |= async_flag;
484 lockmgr(&mp->mnt_lock, LK_RELEASE | LK_INTERLOCK | LK_REENABLE,
485 &mountlist_token, td);
486 if (mp->mnt_kern_flag & MNTK_MWAIT)
490 TAILQ_REMOVE(&mountlist, mp, mnt_list);
491 if ((coveredvp = mp->mnt_vnodecovered) != NULLVP) {
492 coveredvp->v_mountedhere = NULL;
495 mp->mnt_vfc->vfc_refcount--;
496 if (!TAILQ_EMPTY(&mp->mnt_nvnodelist))
497 panic("unmount: dangling vnode");
498 lockmgr(&mp->mnt_lock, LK_RELEASE | LK_INTERLOCK, &mountlist_token, td);
499 if (mp->mnt_kern_flag & MNTK_MWAIT)
501 free((caddr_t)mp, M_MOUNT);
506 * Sync each mounted filesystem.
510 static int syncprt = 0;
511 SYSCTL_INT(_debug, OID_AUTO, syncprt, CTLFLAG_RW, &syncprt, 0, "");
516 sync(struct sync_args *uap)
518 struct thread *td = curthread;
519 struct mount *mp, *nmp;
522 lwkt_gettoken(&mountlist_token);
523 for (mp = TAILQ_FIRST(&mountlist); mp != NULL; mp = nmp) {
524 if (vfs_busy(mp, LK_NOWAIT, &mountlist_token, td)) {
525 nmp = TAILQ_NEXT(mp, mnt_list);
528 if ((mp->mnt_flag & MNT_RDONLY) == 0) {
529 asyncflag = mp->mnt_flag & MNT_ASYNC;
530 mp->mnt_flag &= ~MNT_ASYNC;
531 vfs_msync(mp, MNT_NOWAIT);
532 VFS_SYNC(mp, MNT_NOWAIT, td);
533 mp->mnt_flag |= asyncflag;
535 lwkt_gettoken(&mountlist_token);
536 nmp = TAILQ_NEXT(mp, mnt_list);
539 lwkt_reltoken(&mountlist_token);
542 * XXX don't call vfs_bufstats() yet because that routine
543 * was not imported in the Lite2 merge.
548 #endif /* DIAGNOSTIC */
553 /* XXX PRISON: could be per prison flag */
554 static int prison_quotas;
556 SYSCTL_INT(_kern_prison, OID_AUTO, quotas, CTLFLAG_RW, &prison_quotas, 0, "");
560 * quotactl_args(char *path, int fcmd, int uid, caddr_t arg)
562 * Change filesystem quotas.
566 quotactl(struct quotactl_args *uap)
568 struct thread *td = curthread;
569 struct proc *p = td->td_proc;
575 if (p->p_ucred->cr_prison && !prison_quotas)
577 NDINIT(&nd, NAMEI_LOOKUP, CNP_FOLLOW, UIO_USERSPACE,
578 SCARG(uap, path), td);
579 if ((error = namei(&nd)) != 0)
581 mp = nd.ni_vp->v_mount;
582 NDFREE(&nd, NDF_ONLY_PNBUF);
584 return (VFS_QUOTACTL(mp, SCARG(uap, cmd), SCARG(uap, uid),
585 SCARG(uap, arg), td));
589 kern_statfs(struct nameidata *nd, struct statfs *buf)
591 struct thread *td = curthread;
599 mp = nd->ni_vp->v_mount;
601 NDFREE(nd, NDF_ONLY_PNBUF);
603 error = VFS_STATFS(mp, sp, td);
606 sp->f_flags = mp->mnt_flag & MNT_VISFLAGMASK;
607 bcopy(sp, buf, sizeof(*buf));
608 /* Only root should have access to the fsid's. */
610 buf->f_fsid.val[0] = buf->f_fsid.val[1] = 0;
615 * statfs_args(char *path, struct statfs *buf)
617 * Get filesystem statistics.
620 statfs(struct statfs_args *uap)
622 struct thread *td = curthread;
627 NDINIT(&nd, NAMEI_LOOKUP, CNP_FOLLOW, UIO_USERSPACE, uap->path, td);
629 error = kern_statfs(&nd, &buf);
632 error = copyout(&buf, uap->buf, sizeof(*uap->buf));
637 kern_fstatfs(int fd, struct statfs *buf)
639 struct thread *td = curthread;
640 struct proc *p = td->td_proc;
647 error = getvnode(p->p_fd, fd, &fp);
650 mp = ((struct vnode *)fp->f_data)->v_mount;
654 error = VFS_STATFS(mp, sp, td);
657 sp->f_flags = mp->mnt_flag & MNT_VISFLAGMASK;
658 bcopy(sp, buf, sizeof(*buf));
659 /* Only root should have access to the fsid's. */
661 buf->f_fsid.val[0] = buf->f_fsid.val[1] = 0;
666 * fstatfs_args(int fd, struct statfs *buf)
668 * Get filesystem statistics.
671 fstatfs(struct fstatfs_args *uap)
676 error = kern_fstatfs(uap->fd, &buf);
679 error = copyout(&buf, uap->buf, sizeof(*uap->buf));
684 * getfsstat_args(struct statfs *buf, long bufsize, int flags)
686 * Get statistics on all filesystems.
690 getfsstat(struct getfsstat_args *uap)
692 struct thread *td = curthread;
693 struct mount *mp, *nmp;
696 long count, maxcount, error;
698 maxcount = SCARG(uap, bufsize) / sizeof(struct statfs);
699 sfsp = (caddr_t)SCARG(uap, buf);
701 lwkt_gettoken(&mountlist_token);
702 for (mp = TAILQ_FIRST(&mountlist); mp != NULL; mp = nmp) {
703 if (vfs_busy(mp, LK_NOWAIT, &mountlist_token, td)) {
704 nmp = TAILQ_NEXT(mp, mnt_list);
707 if (sfsp && count < maxcount) {
710 * If MNT_NOWAIT or MNT_LAZY is specified, do not
711 * refresh the fsstat cache. MNT_NOWAIT or MNT_LAZY
712 * overrides MNT_WAIT.
714 if (((SCARG(uap, flags) & (MNT_LAZY|MNT_NOWAIT)) == 0 ||
715 (SCARG(uap, flags) & MNT_WAIT)) &&
716 (error = VFS_STATFS(mp, sp, td))) {
717 lwkt_gettoken(&mountlist_token);
718 nmp = TAILQ_NEXT(mp, mnt_list);
722 sp->f_flags = mp->mnt_flag & MNT_VISFLAGMASK;
723 error = copyout((caddr_t)sp, sfsp, sizeof(*sp));
731 lwkt_gettoken(&mountlist_token);
732 nmp = TAILQ_NEXT(mp, mnt_list);
735 lwkt_reltoken(&mountlist_token);
736 if (sfsp && count > maxcount)
737 uap->sysmsg_result = maxcount;
739 uap->sysmsg_result = count;
744 * fchdir_args(int fd)
746 * Change current working directory to a given file descriptor.
750 fchdir(struct fchdir_args *uap)
752 struct thread *td = curthread;
753 struct proc *p = td->td_proc;
754 struct filedesc *fdp = p->p_fd;
755 struct vnode *vp, *tdp;
760 if ((error = getvnode(fdp, SCARG(uap, fd), &fp)) != 0)
762 vp = (struct vnode *)fp->f_data;
764 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY, td);
765 if (vp->v_type != VDIR)
768 error = VOP_ACCESS(vp, VEXEC, p->p_ucred, td);
769 while (!error && (mp = vp->v_mountedhere) != NULL) {
770 if (vfs_busy(mp, 0, 0, td))
772 error = VFS_ROOT(mp, &tdp);
783 VOP_UNLOCK(vp, 0, td);
790 kern_chdir(struct nameidata *nd)
792 struct thread *td = curthread;
793 struct proc *p = td->td_proc;
794 struct filedesc *fdp = p->p_fd;
797 error = change_dir(nd, td);
800 NDFREE(nd, NDF_ONLY_PNBUF);
802 fdp->fd_cdir = nd->ni_vp;
807 * chdir_args(char *path)
809 * Change current working directory (``.'').
812 chdir(struct chdir_args *uap)
814 struct thread *td = curthread;
818 NDINIT(&nd, NAMEI_LOOKUP, CNP_FOLLOW | CNP_LOCKLEAF, UIO_USERSPACE,
821 error = kern_chdir(&nd);
827 * Helper function for raised chroot(2) security function: Refuse if
828 * any filedescriptors are open directories.
831 chroot_refuse_vdir_fds(fdp)
832 struct filedesc *fdp;
839 for (fd = 0; fd < fdp->fd_nfiles ; fd++) {
840 error = getvnode(fdp, fd, &fp);
843 vp = (struct vnode *)fp->f_data;
844 if (vp->v_type != VDIR)
852 * This sysctl determines if we will allow a process to chroot(2) if it
853 * has a directory open:
854 * 0: disallowed for all processes.
855 * 1: allowed for processes that were not already chroot(2)'ed.
856 * 2: allowed for all processes.
859 static int chroot_allow_open_directories = 1;
861 SYSCTL_INT(_kern, OID_AUTO, chroot_allow_open_directories, CTLFLAG_RW,
862 &chroot_allow_open_directories, 0, "");
865 * chroot_args(char *path)
867 * Change notion of root (``/'') directory.
871 chroot(struct chroot_args *uap)
873 struct thread *td = curthread;
874 struct proc *p = td->td_proc;
875 struct filedesc *fdp = p->p_fd;
880 error = suser_cred(p->p_ucred, PRISON_ROOT);
883 if (chroot_allow_open_directories == 0 ||
884 (chroot_allow_open_directories == 1 && fdp->fd_rdir != rootvnode))
885 error = chroot_refuse_vdir_fds(fdp);
888 NDINIT(&nd, NAMEI_LOOKUP, CNP_FOLLOW | CNP_LOCKLEAF, UIO_USERSPACE,
889 SCARG(uap, path), td);
890 if ((error = change_dir(&nd, td)) != 0)
892 NDFREE(&nd, NDF_ONLY_PNBUF);
894 fdp->fd_rdir = nd.ni_vp;
896 fdp->fd_jdir = nd.ni_vp;
903 * Common routine for chroot and chdir.
906 change_dir(struct nameidata *ndp, struct thread *td)
915 if (vp->v_type != VDIR)
918 error = VOP_ACCESS(vp, VEXEC, ndp->ni_cnd.cn_cred, td);
922 VOP_UNLOCK(vp, 0, td);
927 kern_open(struct nameidata *nd, int oflags, int mode, int *res)
929 struct thread *td = curthread;
930 struct proc *p = td->td_proc;
931 struct filedesc *fdp = p->p_fd;
936 int type, indx, error;
939 if ((oflags & O_ACCMODE) == O_ACCMODE)
941 flags = FFLAGS(oflags);
942 error = falloc(p, &nfp, &indx);
946 cmode = ((mode &~ fdp->fd_cmask) & ALLPERMS) &~ S_ISTXT;
947 p->p_dupfd = -indx - 1; /* XXX check for fdopen */
949 * Bump the ref count to prevent another process from closing
950 * the descriptor while we are blocked in vn_open()
953 error = vn_open(nd, flags, cmode);
956 * release our own reference
961 * handle special fdopen() case. bleh. dupfdopen() is
962 * responsible for dropping the old contents of ofiles[indx]
965 if ((error == ENODEV || error == ENXIO) &&
966 p->p_dupfd >= 0 && /* XXX from fdopen */
968 dupfdopen(fdp, indx, p->p_dupfd, flags, error)) == 0) {
973 * Clean up the descriptor, but only if another thread hadn't
974 * replaced or closed it.
976 if (fdp->fd_ofiles[indx] == fp) {
977 fdp->fd_ofiles[indx] = NULL;
981 if (error == ERESTART)
986 NDFREE(nd, NDF_ONLY_PNBUF);
990 * There should be 2 references on the file, one from the descriptor
991 * table, and one for us.
993 * Handle the case where someone closed the file (via its file
994 * descriptor) while we were blocked. The end result should look
995 * like opening the file succeeded but it was immediately closed.
997 if (fp->f_count == 1) {
998 KASSERT(fdp->fd_ofiles[indx] != fp,
999 ("Open file descriptor lost all refs"));
1000 VOP_UNLOCK(vp, 0, td);
1001 vn_close(vp, flags & FMASK, td);
1007 fp->f_data = (caddr_t)vp;
1008 fp->f_flag = flags & FMASK;
1010 fp->f_type = (vp->v_type == VFIFO ? DTYPE_FIFO : DTYPE_VNODE);
1011 if (flags & (O_EXLOCK | O_SHLOCK)) {
1012 lf.l_whence = SEEK_SET;
1015 if (flags & O_EXLOCK)
1016 lf.l_type = F_WRLCK;
1018 lf.l_type = F_RDLCK;
1020 if ((flags & FNONBLOCK) == 0)
1022 VOP_UNLOCK(vp, 0, td);
1023 if ((error = VOP_ADVLOCK(vp, (caddr_t)fp, F_SETLK, &lf, type)) != 0) {
1025 * lock request failed. Normally close the descriptor
1026 * but handle the case where someone might have dup()d
1027 * it when we weren't looking. One reference is
1028 * owned by the descriptor array, the other by us.
1030 if (fdp->fd_ofiles[indx] == fp) {
1031 fdp->fd_ofiles[indx] = NULL;
1037 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY, td);
1038 fp->f_flag |= FHASLOCK;
1040 /* assert that vn_open created a backing object if one is needed */
1041 KASSERT(!vn_canvmio(vp) || VOP_GETVOBJECT(vp, NULL) == 0,
1042 ("open: vmio vnode has no backing object after vn_open"));
1043 VOP_UNLOCK(vp, 0, td);
1046 * release our private reference, leaving the one associated with the
1047 * descriptor table intact.
1055 * open_args(char *path, int flags, int mode)
1057 * Check permissions, allocate an open file structure,
1058 * and call the device open routine if any.
1061 open(struct open_args *uap)
1063 struct thread *td = curthread;
1064 struct nameidata nd;
1067 NDINIT(&nd, NAMEI_LOOKUP, CNP_FOLLOW, UIO_USERSPACE, uap->path, td);
1069 error = kern_open(&nd, uap->flags, uap->mode, &uap->sysmsg_result);
1075 kern_mknod(struct nameidata *nd, int mode, int dev)
1077 struct thread *td = curthread;
1078 struct proc *p = td->td_proc;
1086 switch (mode & S_IFMT) {
1092 error = suser_cred(p->p_ucred, PRISON_ROOT);
1106 vattr.va_mode = (mode & ALLPERMS) &~ p->p_fd->fd_cmask;
1107 vattr.va_rdev = dev;
1110 switch (mode & S_IFMT) {
1111 case S_IFMT: /* used by badsect to flag bad sectors */
1112 vattr.va_type = VBAD;
1115 vattr.va_type = VCHR;
1118 vattr.va_type = VBLK;
1129 VOP_LEASE(nd->ni_dvp, td, p->p_ucred, LEASE_WRITE);
1131 error = VOP_WHITEOUT(nd->ni_dvp, NCPNULL,
1132 &nd->ni_cnd, NAMEI_CREATE);
1134 error = VOP_MKNOD(nd->ni_dvp, NCPNULL, &nd->ni_vp,
1135 &nd->ni_cnd, &vattr);
1139 NDFREE(nd, NDF_ONLY_PNBUF);
1142 NDFREE(nd, NDF_ONLY_PNBUF);
1143 if (nd->ni_dvp == vp)
1150 ASSERT_VOP_UNLOCKED(nd->ni_dvp, "mknod");
1151 ASSERT_VOP_UNLOCKED(nd->ni_vp, "mknod");
1156 * mknod_args(char *path, int mode, int dev)
1158 * Create a special file.
1161 mknod(struct mknod_args *uap)
1163 struct thread *td = curthread;
1164 struct nameidata nd;
1167 NDINIT(&nd, NAMEI_CREATE, CNP_LOCKPARENT, UIO_USERSPACE, uap->path,
1170 error = kern_mknod(&nd, uap->mode, uap->dev);
1176 kern_mkfifo(struct nameidata *nd, int mode)
1178 struct thread *td = curthread;
1179 struct proc *p = td->td_proc;
1187 if (nd->ni_vp != NULL) {
1188 NDFREE(nd, NDF_ONLY_PNBUF);
1189 if (nd->ni_dvp == nd->ni_vp)
1197 vattr.va_type = VFIFO;
1198 vattr.va_mode = (mode & ALLPERMS) &~ p->p_fd->fd_cmask;
1199 VOP_LEASE(nd->ni_dvp, td, p->p_ucred, LEASE_WRITE);
1200 error = VOP_MKNOD(nd->ni_dvp, NCPNULL, &nd->ni_vp, &nd->ni_cnd, &vattr);
1203 NDFREE(nd, NDF_ONLY_PNBUF);
1209 * mkfifo_args(char *path, int mode)
1211 * Create a named pipe.
1214 mkfifo(struct mkfifo_args *uap)
1216 struct thread *td = curthread;
1217 struct nameidata nd;
1220 NDINIT(&nd, NAMEI_CREATE, CNP_LOCKPARENT, UIO_USERSPACE, uap->path,
1223 error = kern_mkfifo(&nd, uap->mode);
1229 kern_link(struct nameidata *nd, struct nameidata *linknd)
1231 struct thread *td = curthread;
1232 struct proc *p = td->td_proc;
1240 NDFREE(nd, NDF_ONLY_PNBUF);
1242 if (vp->v_type == VDIR)
1243 error = EPERM; /* POSIX */
1245 error = namei(linknd);
1247 if (linknd->ni_vp != NULL) {
1249 vrele(linknd->ni_vp);
1252 VOP_LEASE(linknd->ni_dvp, td, p->p_ucred,
1254 VOP_LEASE(vp, td, p->p_ucred, LEASE_WRITE);
1255 error = VOP_LINK(linknd->ni_dvp, NCPNULL, vp,
1258 NDFREE(linknd, NDF_ONLY_PNBUF);
1259 if (linknd->ni_dvp == linknd->ni_vp)
1260 vrele(linknd->ni_dvp);
1262 vput(linknd->ni_dvp);
1263 ASSERT_VOP_UNLOCKED(linknd->ni_dvp, "link");
1264 ASSERT_VOP_UNLOCKED(linknd->ni_vp, "link");
1272 * link_args(char *path, char *link)
1274 * Make a hard file link.
1277 link(struct link_args *uap)
1279 struct thread *td = curthread;
1280 struct nameidata nd, linknd;
1283 NDINIT(&nd, NAMEI_LOOKUP, CNP_FOLLOW | CNP_NOOBJ, UIO_USERSPACE,
1285 NDINIT(&linknd, NAMEI_CREATE, CNP_LOCKPARENT | CNP_NOOBJ,
1286 UIO_USERSPACE, uap->link, td);
1288 error = kern_link(&nd, &linknd);
1294 kern_symlink(char *path, struct nameidata *nd)
1296 struct thread *td = curthread;
1297 struct proc *p = td->td_proc;
1306 NDFREE(nd, NDF_ONLY_PNBUF);
1307 if (nd->ni_dvp == nd->ni_vp)
1315 vattr.va_mode = ACCESSPERMS &~ p->p_fd->fd_cmask;
1316 VOP_LEASE(nd->ni_dvp, td, p->p_ucred, LEASE_WRITE);
1317 error = VOP_SYMLINK(nd->ni_dvp, NCPNULL, &nd->ni_vp, &nd->ni_cnd,
1319 NDFREE(nd, NDF_ONLY_PNBUF);
1323 ASSERT_VOP_UNLOCKED(nd->ni_dvp, "symlink");
1324 ASSERT_VOP_UNLOCKED(nd->ni_vp, "symlink");
1330 * symlink(char *path, char *link)
1332 * Make a symbolic link.
1335 symlink(struct symlink_args *uap)
1337 struct thread *td = curthread;
1338 struct nameidata nd;
1342 path = zalloc(namei_zone);
1343 error = copyinstr(uap->path, path, MAXPATHLEN, NULL);
1346 NDINIT(&nd, NAMEI_CREATE, CNP_LOCKPARENT | CNP_NOOBJ, UIO_USERSPACE,
1349 error = kern_symlink(path, &nd);
1351 zfree(namei_zone, path);
1356 * undelete_args(char *path)
1358 * Delete a whiteout from the filesystem.
1362 undelete(struct undelete_args *uap)
1364 struct thread *td = curthread;
1365 struct proc *p = td->td_proc;
1367 struct nameidata nd;
1370 NDINIT(&nd, NAMEI_DELETE, CNP_LOCKPARENT | CNP_DOWHITEOUT, UIO_USERSPACE,
1371 SCARG(uap, path), td);
1376 if (nd.ni_vp != NULLVP || !(nd.ni_cnd.cn_flags & CNP_ISWHITEOUT)) {
1377 NDFREE(&nd, NDF_ONLY_PNBUF);
1378 if (nd.ni_dvp == nd.ni_vp)
1387 VOP_LEASE(nd.ni_dvp, td, p->p_ucred, LEASE_WRITE);
1388 error = VOP_WHITEOUT(nd.ni_dvp, NCPNULL, &nd.ni_cnd, NAMEI_DELETE);
1389 NDFREE(&nd, NDF_ONLY_PNBUF);
1391 ASSERT_VOP_UNLOCKED(nd.ni_dvp, "undelete");
1392 ASSERT_VOP_UNLOCKED(nd.ni_vp, "undelete");
1397 kern_unlink(struct nameidata *nd)
1399 struct thread *td = curthread;
1400 struct proc *p = td->td_proc;
1409 VOP_LEASE(vp, td, p->p_ucred, LEASE_WRITE);
1410 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY, td);
1412 if (vp->v_type == VDIR)
1413 error = EPERM; /* POSIX */
1416 * The root of a mounted filesystem cannot be deleted.
1418 * XXX: can this only be a VDIR case?
1420 if (vp->v_flag & VROOT)
1425 VOP_LEASE(nd->ni_dvp, td, p->p_ucred, LEASE_WRITE);
1426 error = VOP_REMOVE(nd->ni_dvp, NCPNULL, vp, &nd->ni_cnd);
1428 NDFREE(nd, NDF_ONLY_PNBUF);
1429 if (nd->ni_dvp == vp)
1435 ASSERT_VOP_UNLOCKED(nd->ni_dvp, "unlink");
1436 ASSERT_VOP_UNLOCKED(nd->ni_vp, "unlink");
1441 * unlink_args(char *path)
1443 * Delete a name from the filesystem.
1446 unlink(struct unlink_args *uap)
1448 struct thread *td = curthread;
1449 struct nameidata nd;
1452 NDINIT(&nd, NAMEI_DELETE, CNP_LOCKPARENT, UIO_USERSPACE, uap->path,
1455 error = kern_unlink(&nd);
1461 kern_lseek(int fd, off_t offset, int whence, off_t *res)
1463 struct thread *td = curthread;
1464 struct proc *p = td->td_proc;
1465 struct filedesc *fdp = p->p_fd;
1470 if (fd >= fdp->fd_nfiles ||
1471 (fp = fdp->fd_ofiles[fd]) == NULL)
1473 if (fp->f_type != DTYPE_VNODE)
1477 fp->f_offset += offset;
1480 error=VOP_GETATTR((struct vnode *)fp->f_data, &vattr, td);
1483 fp->f_offset = offset + vattr.va_size;
1486 fp->f_offset = offset;
1491 *res = fp->f_offset;
1496 * lseek_args(int fd, int pad, off_t offset, int whence)
1498 * Reposition read/write file offset.
1501 lseek(struct lseek_args *uap)
1505 error = kern_lseek(uap->fd, uap->offset, uap->whence,
1506 &uap->sysmsg_offset);
1512 kern_access(struct nameidata *nd, int aflags)
1514 struct thread *td = curthread;
1515 struct proc *p = td->td_proc;
1516 struct ucred *cred, *tmpcred;
1522 * Create and modify a temporary credential instead of one that
1523 * is potentially shared. This could also mess up socket
1524 * buffer accounting which can run in an interrupt context.
1526 tmpcred = crdup(cred);
1527 tmpcred->cr_uid = p->p_ucred->cr_ruid;
1528 tmpcred->cr_groups[0] = p->p_ucred->cr_rgid;
1529 p->p_ucred = tmpcred;
1530 nd->ni_cnd.cn_cred = tmpcred;
1536 /* Flags == 0 means only check for existence. */
1545 if ((flags & VWRITE) == 0 || (error = vn_writechk(vp)) == 0)
1546 error = VOP_ACCESS(vp, flags, tmpcred, td);
1548 NDFREE(nd, NDF_ONLY_PNBUF);
1557 * access_args(char *path, int flags)
1559 * Check access permissions.
1562 access(struct access_args *uap)
1564 struct thread *td = curthread;
1565 struct nameidata nd;
1568 NDINIT(&nd, NAMEI_LOOKUP, CNP_FOLLOW | CNP_LOCKLEAF | CNP_NOOBJ,
1569 UIO_USERSPACE, uap->path, td);
1571 error = kern_access(&nd, uap->flags);
1577 kern_stat(struct nameidata *nd, struct stat *st)
1579 struct thread *td = curthread;
1585 error = vn_stat(nd->ni_vp, st, td);
1586 NDFREE(nd, NDF_ONLY_PNBUF);
1592 * stat_args(char *path, struct stat *ub)
1594 * Get file status; this version follows links.
1597 stat(struct stat_args *uap)
1599 struct thread *td = curthread;
1600 struct nameidata nd;
1604 NDINIT(&nd, NAMEI_LOOKUP, CNP_FOLLOW | CNP_LOCKLEAF | CNP_NOOBJ,
1605 UIO_USERSPACE, uap->path, td);
1607 error = kern_stat(&nd, &st);
1610 error = copyout(&st, uap->ub, sizeof(*uap->ub));
1615 * lstat_args(char *path, struct stat *ub)
1617 * Get file status; this version does not follow links.
1620 lstat(struct lstat_args *uap)
1622 struct thread *td = curthread;
1623 struct nameidata nd;
1627 NDINIT(&nd, NAMEI_LOOKUP, CNP_LOCKLEAF | CNP_NOOBJ,
1628 UIO_USERSPACE, SCARG(uap, path), td);
1630 error = kern_stat(&nd, &st);
1633 error = copyout(&st, uap->ub, sizeof(*uap->ub));
1642 nsb->st_dev = sb->st_dev;
1643 nsb->st_ino = sb->st_ino;
1644 nsb->st_mode = sb->st_mode;
1645 nsb->st_nlink = sb->st_nlink;
1646 nsb->st_uid = sb->st_uid;
1647 nsb->st_gid = sb->st_gid;
1648 nsb->st_rdev = sb->st_rdev;
1649 nsb->st_atimespec = sb->st_atimespec;
1650 nsb->st_mtimespec = sb->st_mtimespec;
1651 nsb->st_ctimespec = sb->st_ctimespec;
1652 nsb->st_size = sb->st_size;
1653 nsb->st_blocks = sb->st_blocks;
1654 nsb->st_blksize = sb->st_blksize;
1655 nsb->st_flags = sb->st_flags;
1656 nsb->st_gen = sb->st_gen;
1657 nsb->st_qspare[0] = sb->st_qspare[0];
1658 nsb->st_qspare[1] = sb->st_qspare[1];
1662 * nstat_args(char *path, struct nstat *ub)
1666 nstat(struct nstat_args *uap)
1668 struct thread *td = curthread;
1672 struct nameidata nd;
1674 NDINIT(&nd, NAMEI_LOOKUP, CNP_FOLLOW | CNP_LOCKLEAF | CNP_NOOBJ,
1675 UIO_USERSPACE, SCARG(uap, path), td);
1676 if ((error = namei(&nd)) != 0)
1678 NDFREE(&nd, NDF_ONLY_PNBUF);
1679 error = vn_stat(nd.ni_vp, &sb, td);
1683 cvtnstat(&sb, &nsb);
1684 error = copyout((caddr_t)&nsb, (caddr_t)SCARG(uap, ub), sizeof (nsb));
1689 * lstat_args(char *path, struct stat *ub)
1691 * Get file status; this version does not follow links.
1695 nlstat(struct nlstat_args *uap)
1697 struct thread *td = curthread;
1702 struct nameidata nd;
1704 NDINIT(&nd, NAMEI_LOOKUP, CNP_LOCKLEAF | CNP_NOOBJ,
1705 UIO_USERSPACE, SCARG(uap, path), td);
1706 if ((error = namei(&nd)) != 0)
1709 NDFREE(&nd, NDF_ONLY_PNBUF);
1710 error = vn_stat(vp, &sb, td);
1714 cvtnstat(&sb, &nsb);
1715 error = copyout((caddr_t)&nsb, (caddr_t)SCARG(uap, ub), sizeof (nsb));
1720 * pathconf_Args(char *path, int name)
1722 * Get configurable pathname variables.
1726 pathconf(struct pathconf_args *uap)
1728 struct thread *td = curthread;
1730 struct nameidata nd;
1732 NDINIT(&nd, NAMEI_LOOKUP, CNP_FOLLOW | CNP_LOCKLEAF | CNP_NOOBJ,
1733 UIO_USERSPACE, SCARG(uap, path), td);
1734 if ((error = namei(&nd)) != 0)
1736 NDFREE(&nd, NDF_ONLY_PNBUF);
1737 error = VOP_PATHCONF(nd.ni_vp, SCARG(uap, name), uap->sysmsg_fds);
1744 * kern_readlink isn't properly split yet. There is a copyin burried
1745 * in VOP_READLINK().
1748 kern_readlink(struct nameidata *nd, char *buf, int count, int *res)
1750 struct thread *td = curthread;
1751 struct proc *p = td->td_proc;
1760 NDFREE(nd, NDF_ONLY_PNBUF);
1762 if (vp->v_type != VLNK)
1765 aiov.iov_base = buf;
1766 aiov.iov_len = count;
1767 auio.uio_iov = &aiov;
1768 auio.uio_iovcnt = 1;
1769 auio.uio_offset = 0;
1770 auio.uio_rw = UIO_READ;
1771 auio.uio_segflg = UIO_USERSPACE;
1773 auio.uio_resid = count;
1774 error = VOP_READLINK(vp, &auio, p->p_ucred);
1777 *res = count - auio.uio_resid;
1782 * readlink_args(char *path, char *buf, int count)
1784 * Return target name of a symbolic link.
1787 readlink(struct readlink_args *uap)
1789 struct thread *td = curthread;
1790 struct nameidata nd;
1793 NDINIT(&nd, NAMEI_LOOKUP, CNP_LOCKLEAF | CNP_NOOBJ, UIO_USERSPACE,
1796 error = kern_readlink(&nd, uap->buf, uap->count,
1797 &uap->sysmsg_result);
1803 setfflags(struct vnode *vp, int flags)
1805 struct thread *td = curthread;
1806 struct proc *p = td->td_proc;
1811 * Prevent non-root users from setting flags on devices. When
1812 * a device is reused, users can retain ownership of the device
1813 * if they are allowed to set flags and programs assume that
1814 * chown can't fail when done as root.
1816 if ((vp->v_type == VCHR || vp->v_type == VBLK) &&
1817 ((error = suser_cred(p->p_ucred, PRISON_ROOT)) != 0))
1820 VOP_LEASE(vp, td, p->p_ucred, LEASE_WRITE);
1821 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY, td);
1823 vattr.va_flags = flags;
1824 error = VOP_SETATTR(vp, &vattr, p->p_ucred, td);
1825 VOP_UNLOCK(vp, 0, td);
1830 * chflags(char *path, int flags)
1832 * Change flags of a file given a path name.
1836 chflags(struct chflags_args *uap)
1838 struct thread *td = curthread;
1840 struct nameidata nd;
1842 NDINIT(&nd, NAMEI_LOOKUP, CNP_FOLLOW, UIO_USERSPACE,
1843 SCARG(uap, path), td);
1844 if ((error = namei(&nd)) != 0)
1846 NDFREE(&nd, NDF_ONLY_PNBUF);
1847 error = setfflags(nd.ni_vp, SCARG(uap, flags));
1853 * fchflags_args(int fd, int flags)
1855 * Change flags of a file given a file descriptor.
1859 fchflags(struct fchflags_args *uap)
1861 struct thread *td = curthread;
1862 struct proc *p = td->td_proc;
1866 if ((error = getvnode(p->p_fd, SCARG(uap, fd), &fp)) != 0)
1868 return setfflags((struct vnode *) fp->f_data, SCARG(uap, flags));
1872 setfmode(struct vnode *vp, int mode)
1874 struct thread *td = curthread;
1875 struct proc *p = td->td_proc;
1879 VOP_LEASE(vp, td, p->p_ucred, LEASE_WRITE);
1880 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY, td);
1882 vattr.va_mode = mode & ALLPERMS;
1883 error = VOP_SETATTR(vp, &vattr, p->p_ucred, td);
1884 VOP_UNLOCK(vp, 0, td);
1889 kern_chmod(struct nameidata *nd, int mode)
1896 NDFREE(nd, NDF_ONLY_PNBUF);
1897 error = setfmode(nd->ni_vp, mode);
1903 * chmod_args(char *path, int mode)
1905 * Change mode of a file given path name.
1909 chmod(struct chmod_args *uap)
1911 struct thread *td = curthread;
1912 struct nameidata nd;
1915 NDINIT(&nd, NAMEI_LOOKUP, CNP_FOLLOW, UIO_USERSPACE, uap->path, td);
1917 error = kern_chmod(&nd, uap->mode);
1923 * lchmod_args(char *path, int mode)
1925 * Change mode of a file given path name (don't follow links.)
1929 lchmod(struct lchmod_args *uap)
1931 struct thread *td = curthread;
1933 struct nameidata nd;
1935 NDINIT(&nd, NAMEI_LOOKUP, 0, UIO_USERSPACE, SCARG(uap, path), td);
1936 if ((error = namei(&nd)) != 0)
1938 NDFREE(&nd, NDF_ONLY_PNBUF);
1939 error = setfmode(nd.ni_vp, SCARG(uap, mode));
1945 * fchmod_args(int fd, int mode)
1947 * Change mode of a file given a file descriptor.
1951 fchmod(struct fchmod_args *uap)
1953 struct thread *td = curthread;
1954 struct proc *p = td->td_proc;
1958 if ((error = getvnode(p->p_fd, SCARG(uap, fd), &fp)) != 0)
1960 return setfmode((struct vnode *)fp->f_data, SCARG(uap, mode));
1964 setfown(struct vnode *vp, uid_t uid, gid_t gid)
1966 struct thread *td = curthread;
1967 struct proc *p = td->td_proc;
1971 VOP_LEASE(vp, td, p->p_ucred, LEASE_WRITE);
1972 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY, td);
1976 error = VOP_SETATTR(vp, &vattr, p->p_ucred, td);
1977 VOP_UNLOCK(vp, 0, td);
1982 kern_chown(struct nameidata *nd, int uid, int gid)
1989 NDFREE(nd, NDF_ONLY_PNBUF);
1990 error = setfown(nd->ni_vp, uid, gid);
1996 * chown(char *path, int uid, int gid)
1998 * Set ownership given a path name.
2001 chown(struct chown_args *uap)
2003 struct thread *td = curthread;
2004 struct nameidata nd;
2007 NDINIT(&nd, NAMEI_LOOKUP, CNP_FOLLOW, UIO_USERSPACE, uap->path, td);
2009 error = kern_chown(&nd, uap->uid, uap->gid);
2015 * lchown_args(char *path, int uid, int gid)
2017 * Set ownership given a path name, do not cross symlinks.
2020 lchown(struct lchown_args *uap)
2022 struct thread *td = curthread;
2024 struct nameidata nd;
2026 NDINIT(&nd, NAMEI_LOOKUP, 0, UIO_USERSPACE, uap->path, td);
2028 error = kern_chown(&nd, uap->uid, uap->gid);
2034 * fchown_args(int fd, int uid, int gid)
2036 * Set ownership given a file descriptor.
2040 fchown(struct fchown_args *uap)
2042 struct thread *td = curthread;
2043 struct proc *p = td->td_proc;
2047 if ((error = getvnode(p->p_fd, SCARG(uap, fd), &fp)) != 0)
2049 return setfown((struct vnode *)fp->f_data,
2050 SCARG(uap, uid), SCARG(uap, gid));
2054 getutimes(const struct timeval *tvp, struct timespec *tsp)
2056 struct timeval tv[2];
2060 TIMEVAL_TO_TIMESPEC(&tv[0], &tsp[0]);
2063 TIMEVAL_TO_TIMESPEC(&tvp[0], &tsp[0]);
2064 TIMEVAL_TO_TIMESPEC(&tvp[1], &tsp[1]);
2070 setutimes(struct vnode *vp, const struct timespec *ts, int nullflag)
2072 struct thread *td = curthread;
2073 struct proc *p = td->td_proc;
2077 VOP_LEASE(vp, td, p->p_ucred, LEASE_WRITE);
2078 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY, td);
2080 vattr.va_atime = ts[0];
2081 vattr.va_mtime = ts[1];
2083 vattr.va_vaflags |= VA_UTIMES_NULL;
2084 error = VOP_SETATTR(vp, &vattr, p->p_ucred, td);
2085 VOP_UNLOCK(vp, 0, td);
2090 kern_utimes(struct nameidata *nd, struct timeval *tptr)
2092 struct timespec ts[2];
2095 error = getutimes(tptr, ts);
2101 NDFREE(nd, NDF_ONLY_PNBUF);
2102 error = setutimes(nd->ni_vp, ts, tptr == NULL);
2108 * utimes_args(char *path, struct timeval *tptr)
2110 * Set the access and modification times of a file.
2113 utimes(struct utimes_args *uap)
2115 struct thread *td = curthread;
2116 struct timeval tv[2];
2117 struct nameidata nd;
2121 error = copyin(uap->tptr, tv, sizeof(tv));
2125 NDINIT(&nd, NAMEI_LOOKUP, CNP_FOLLOW, UIO_USERSPACE, uap->path, td);
2127 error = kern_utimes(&nd, uap->tptr ? tv : NULL);
2133 * lutimes_args(char *path, struct timeval *tptr)
2135 * Set the access and modification times of a file.
2138 lutimes(struct lutimes_args *uap)
2140 struct thread *td = curthread;
2141 struct timeval tv[2];
2142 struct nameidata nd;
2146 error = copyin(uap->tptr, tv, sizeof(tv));
2150 NDINIT(&nd, NAMEI_LOOKUP, 0, UIO_USERSPACE, uap->path, td);
2152 error = kern_utimes(&nd, uap->tptr ? tv : NULL);
2158 kern_futimes(int fd, struct timeval *tptr)
2160 struct thread *td = curthread;
2161 struct proc *p = td->td_proc;
2162 struct timespec ts[2];
2166 error = getutimes(tptr, ts);
2169 error = getvnode(p->p_fd, fd, &fp);
2172 error = setutimes((struct vnode *)fp->f_data, ts, tptr == NULL);
2177 * futimes_args(int fd, struct timeval *tptr)
2179 * Set the access and modification times of a file.
2182 futimes(struct futimes_args *uap)
2184 struct timeval tv[2];
2188 error = copyin(uap->tptr, tv, sizeof(tv));
2193 error = kern_futimes(uap->fd, uap->tptr ? tv : NULL);
2199 kern_truncate(struct nameidata* nd, off_t length)
2201 struct thread *td = curthread;
2202 struct proc *p = td->td_proc;
2209 if ((error = namei(nd)) != 0)
2212 NDFREE(nd, NDF_ONLY_PNBUF);
2213 VOP_LEASE(vp, td, p->p_ucred, LEASE_WRITE);
2214 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY, td);
2215 if (vp->v_type == VDIR)
2217 else if ((error = vn_writechk(vp)) == 0 &&
2218 (error = VOP_ACCESS(vp, VWRITE, p->p_ucred, td)) == 0) {
2220 vattr.va_size = length;
2221 error = VOP_SETATTR(vp, &vattr, p->p_ucred, td);
2228 * truncate(char *path, int pad, off_t length)
2230 * Truncate a file given its path name.
2233 truncate(struct truncate_args *uap)
2235 struct thread *td = curthread;
2236 struct nameidata nd;
2239 NDINIT(&nd, NAMEI_LOOKUP, CNP_FOLLOW, UIO_USERSPACE, uap->path, td);
2241 error = kern_truncate(&nd, uap->length);
2247 kern_ftruncate(int fd, off_t length)
2249 struct thread *td = curthread;
2250 struct proc *p = td->td_proc;
2258 if ((error = getvnode(p->p_fd, fd, &fp)) != 0)
2260 if ((fp->f_flag & FWRITE) == 0)
2262 vp = (struct vnode *)fp->f_data;
2263 VOP_LEASE(vp, td, p->p_ucred, LEASE_WRITE);
2264 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY, td);
2265 if (vp->v_type == VDIR)
2267 else if ((error = vn_writechk(vp)) == 0) {
2269 vattr.va_size = length;
2270 error = VOP_SETATTR(vp, &vattr, fp->f_cred, td);
2272 VOP_UNLOCK(vp, 0, td);
2277 * ftruncate_args(int fd, int pad, off_t length)
2279 * Truncate a file given a file descriptor.
2282 ftruncate(struct ftruncate_args *uap)
2286 error = kern_ftruncate(uap->fd, uap->length);
2294 * Sync an open file.
2298 fsync(struct fsync_args *uap)
2300 struct thread *td = curthread;
2301 struct proc *p = td->td_proc;
2307 if ((error = getvnode(p->p_fd, SCARG(uap, fd), &fp)) != 0)
2309 vp = (struct vnode *)fp->f_data;
2310 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY, td);
2311 if (VOP_GETVOBJECT(vp, &obj) == 0)
2312 vm_object_page_clean(obj, 0, 0, 0);
2313 if ((error = VOP_FSYNC(vp, MNT_WAIT, td)) == 0 &&
2314 vp->v_mount && (vp->v_mount->mnt_flag & MNT_SOFTDEP) &&
2316 error = (*bioops.io_fsync)(vp);
2317 VOP_UNLOCK(vp, 0, td);
2322 kern_rename(struct nameidata *fromnd, struct nameidata *tond)
2324 struct thread *td = curthread;
2325 struct proc *p = td->td_proc;
2326 struct vnode *tvp, *fvp, *tdvp;
2330 error = namei(fromnd);
2333 fvp = fromnd->ni_vp;
2334 if (fromnd->ni_vp->v_type == VDIR)
2335 tond->ni_cnd.cn_flags |= CNP_WILLBEDIR;
2336 error = namei(tond);
2338 /* Translate error code for rename("dir1", "dir2/."). */
2339 if (error == EISDIR && fvp->v_type == VDIR)
2341 NDFREE(fromnd, NDF_ONLY_PNBUF);
2342 vrele(fromnd->ni_dvp);
2346 tdvp = tond->ni_dvp;
2349 if (fvp->v_type == VDIR && tvp->v_type != VDIR) {
2352 } else if (fvp->v_type != VDIR && tvp->v_type == VDIR) {
2360 * If the source is the same as the destination (that is, if they
2361 * are links to the same vnode), then there is nothing to do.
2367 VOP_LEASE(tdvp, td, p->p_ucred, LEASE_WRITE);
2368 if (fromnd->ni_dvp != tdvp) {
2369 VOP_LEASE(fromnd->ni_dvp, td, p->p_ucred, LEASE_WRITE);
2372 VOP_LEASE(tvp, td, p->p_ucred, LEASE_WRITE);
2374 error = VOP_RENAME(fromnd->ni_dvp, NCPNULL, fromnd->ni_vp,
2375 &fromnd->ni_cnd, tond->ni_dvp, NCPNULL, tond->ni_vp,
2377 NDFREE(fromnd, NDF_ONLY_PNBUF);
2378 NDFREE(tond, NDF_ONLY_PNBUF);
2380 NDFREE(fromnd, NDF_ONLY_PNBUF);
2381 NDFREE(tond, NDF_ONLY_PNBUF);
2388 vrele(fromnd->ni_dvp);
2391 vrele(tond->ni_startdir);
2392 ASSERT_VOP_UNLOCKED(fromnd->ni_dvp, "rename");
2393 ASSERT_VOP_UNLOCKED(fromnd->ni_vp, "rename");
2394 ASSERT_VOP_UNLOCKED(tond->ni_dvp, "rename");
2395 ASSERT_VOP_UNLOCKED(tond->ni_vp, "rename");
2397 if (fromnd->ni_startdir)
2398 vrele(fromnd->ni_startdir);
2405 * rename_args(char *from, char *to)
2407 * Rename files. Source and destination must either both be directories,
2408 * or both not be directories. If target is a directory, it must be empty.
2411 rename(struct rename_args *uap)
2413 struct thread *td = curthread;
2414 struct nameidata fromnd, tond;
2417 NDINIT(&fromnd, NAMEI_DELETE, CNP_WANTPARENT | CNP_SAVESTART,
2418 UIO_USERSPACE, uap->from, td);
2419 NDINIT(&tond, NAMEI_RENAME,
2420 CNP_LOCKPARENT | CNP_LOCKLEAF | CNP_NOCACHE |
2421 CNP_SAVESTART | CNP_NOOBJ,
2422 UIO_USERSPACE, uap->to, td);
2424 error = kern_rename(&fromnd, &tond);
2430 kern_mkdir(struct nameidata *nd, int mode)
2432 struct thread *td = curthread;
2433 struct proc *p = td->td_proc;
2439 nd->ni_cnd.cn_flags |= CNP_WILLBEDIR;
2445 NDFREE(nd, NDF_ONLY_PNBUF);
2446 if (nd->ni_dvp == vp)
2454 vattr.va_type = VDIR;
2455 vattr.va_mode = (mode & ACCESSPERMS) &~ p->p_fd->fd_cmask;
2456 VOP_LEASE(nd->ni_dvp, td, p->p_ucred, LEASE_WRITE);
2457 error = VOP_MKDIR(nd->ni_dvp, NCPNULL, &nd->ni_vp, &nd->ni_cnd,
2459 NDFREE(nd, NDF_ONLY_PNBUF);
2463 ASSERT_VOP_UNLOCKED(nd->ni_dvp, "mkdir");
2464 ASSERT_VOP_UNLOCKED(nd->ni_vp, "mkdir");
2469 * mkdir_args(char *path, int mode)
2471 * Make a directory file.
2475 mkdir(struct mkdir_args *uap)
2477 struct thread *td = curthread;
2478 struct nameidata nd;
2481 NDINIT(&nd, NAMEI_CREATE, CNP_LOCKPARENT, UIO_USERSPACE, uap->path,
2484 error = kern_mkdir(&nd, uap->mode);
2490 kern_rmdir(struct nameidata *nd)
2492 struct thread *td = curthread;
2493 struct proc *p = td->td_proc;
2502 if (vp->v_type != VDIR) {
2507 * No rmdir "." please.
2509 if (nd->ni_dvp == vp) {
2514 * The root of a mounted filesystem cannot be deleted.
2516 if (vp->v_flag & VROOT)
2519 VOP_LEASE(nd->ni_dvp, td, p->p_ucred, LEASE_WRITE);
2520 VOP_LEASE(vp, td, p->p_ucred, LEASE_WRITE);
2521 error = VOP_RMDIR(nd->ni_dvp, NCPNULL, nd->ni_vp,
2525 NDFREE(nd, NDF_ONLY_PNBUF);
2526 if (nd->ni_dvp == vp)
2532 ASSERT_VOP_UNLOCKED(nd->ni_dvp, "rmdir");
2533 ASSERT_VOP_UNLOCKED(nd->ni_vp, "rmdir");
2538 * rmdir_args(char *path)
2540 * Remove a directory file.
2544 rmdir(struct rmdir_args *uap)
2546 struct thread *td = curthread;
2547 struct nameidata nd;
2550 NDINIT(&nd, NAMEI_DELETE, CNP_LOCKPARENT | CNP_LOCKLEAF,
2551 UIO_USERSPACE, uap->path, td);
2553 error = kern_rmdir(&nd);
2559 kern_getdirentries(int fd, char *buf, u_int count, long *basep, int *res)
2561 struct thread *td = curthread;
2562 struct proc *p = td->td_proc;
2570 if ((error = getvnode(p->p_fd, fd, &fp)) != 0)
2572 if ((fp->f_flag & FREAD) == 0)
2574 vp = (struct vnode *)fp->f_data;
2576 if (vp->v_type != VDIR)
2578 aiov.iov_base = buf;
2579 aiov.iov_len = count;
2580 auio.uio_iov = &aiov;
2581 auio.uio_iovcnt = 1;
2582 auio.uio_rw = UIO_READ;
2583 auio.uio_segflg = UIO_USERSPACE;
2585 auio.uio_resid = count;
2586 /* vn_lock(vp, LK_SHARED | LK_RETRY, td); */
2587 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY, td);
2588 loff = auio.uio_offset = fp->f_offset;
2589 error = VOP_READDIR(vp, &auio, fp->f_cred, &eofflag, NULL, NULL);
2590 fp->f_offset = auio.uio_offset;
2591 VOP_UNLOCK(vp, 0, td);
2594 if (count == auio.uio_resid) {
2595 if (union_dircheckp) {
2596 error = union_dircheckp(td, &vp, fp);
2602 if ((vp->v_flag & VROOT) &&
2603 (vp->v_mount->mnt_flag & MNT_UNION)) {
2604 struct vnode *tvp = vp;
2605 vp = vp->v_mount->mnt_vnodecovered;
2607 fp->f_data = (caddr_t) vp;
2616 *res = count - auio.uio_resid;
2621 * getdirentries_args(int fd, char *buf, u_int conut, long *basep)
2623 * Read a block of directory entries in a file system independent format.
2626 getdirentries(struct getdirentries_args *uap)
2631 error = kern_getdirentries(uap->fd, uap->buf, uap->count, &base,
2632 &uap->sysmsg_result);
2635 error = copyout(&base, uap->basep, sizeof(*uap->basep));
2640 * getdents_args(int fd, char *buf, size_t count)
2643 getdents(struct getdents_args *uap)
2647 error = kern_getdirentries(uap->fd, uap->buf, uap->count, NULL,
2648 &uap->sysmsg_result);
2654 * umask(int newmask)
2656 * Set the mode mask for creation of filesystem nodes.
2661 umask(struct umask_args *uap)
2663 struct thread *td = curthread;
2664 struct proc *p = td->td_proc;
2665 struct filedesc *fdp;
2668 uap->sysmsg_result = fdp->fd_cmask;
2669 fdp->fd_cmask = SCARG(uap, newmask) & ALLPERMS;
2674 * revoke(char *path)
2676 * Void all references to file by ripping underlying filesystem
2681 revoke(struct revoke_args *uap)
2683 struct thread *td = curthread;
2684 struct proc *p = td->td_proc;
2688 struct nameidata nd;
2690 NDINIT(&nd, NAMEI_LOOKUP, CNP_FOLLOW, UIO_USERSPACE, SCARG(uap, path), td);
2691 if ((error = namei(&nd)) != 0)
2694 NDFREE(&nd, NDF_ONLY_PNBUF);
2695 if (vp->v_type != VCHR && vp->v_type != VBLK) {
2699 if ((error = VOP_GETATTR(vp, &vattr, td)) != 0)
2701 if (p->p_ucred->cr_uid != vattr.va_uid &&
2702 (error = suser_cred(p->p_ucred, PRISON_ROOT)))
2705 VOP_REVOKE(vp, REVOKEALL);
2712 * Convert a user file descriptor to a kernel file entry.
2715 getvnode(struct filedesc *fdp, int fd, struct file **fpp)
2719 if ((u_int)fd >= fdp->fd_nfiles ||
2720 (fp = fdp->fd_ofiles[fd]) == NULL)
2722 if (fp->f_type != DTYPE_VNODE && fp->f_type != DTYPE_FIFO)
2728 * getfh_args(char *fname, fhandle_t *fhp)
2730 * Get (NFS) file handle
2733 getfh(struct getfh_args *uap)
2735 struct thread *td = curthread;
2736 struct nameidata nd;
2742 * Must be super user
2747 NDINIT(&nd, NAMEI_LOOKUP, CNP_FOLLOW | CNP_LOCKLEAF, UIO_USERSPACE, uap->fname, td);
2751 NDFREE(&nd, NDF_ONLY_PNBUF);
2753 bzero(&fh, sizeof(fh));
2754 fh.fh_fsid = vp->v_mount->mnt_stat.f_fsid;
2755 error = VFS_VPTOFH(vp, &fh.fh_fid);
2759 error = copyout(&fh, uap->fhp, sizeof (fh));
2764 * fhopen_args(const struct fhandle *u_fhp, int flags)
2766 * syscall for the rpc.lockd to use to translate a NFS file handle into
2767 * an open descriptor.
2769 * warning: do not remove the suser() call or this becomes one giant
2773 fhopen(struct fhopen_args *uap)
2775 struct thread *td = curthread;
2776 struct proc *p = td->td_proc;
2781 struct vattr *vap = &vat;
2784 struct filedesc *fdp = p->p_fd;
2785 int fmode, mode, error, type;
2790 * Must be super user
2796 fmode = FFLAGS(SCARG(uap, flags));
2797 /* why not allow a non-read/write open for our lockd? */
2798 if (((fmode & (FREAD | FWRITE)) == 0) || (fmode & O_CREAT))
2800 error = copyin(SCARG(uap,u_fhp), &fhp, sizeof(fhp));
2803 /* find the mount point */
2804 mp = vfs_getvfs(&fhp.fh_fsid);
2807 /* now give me my vnode, it gets returned to me locked */
2808 error = VFS_FHTOVP(mp, &fhp.fh_fid, &vp);
2812 * from now on we have to make sure not
2813 * to forget about the vnode
2814 * any error that causes an abort must vput(vp)
2815 * just set error = err and 'goto bad;'.
2821 if (vp->v_type == VLNK) {
2825 if (vp->v_type == VSOCK) {
2830 if (fmode & (FWRITE | O_TRUNC)) {
2831 if (vp->v_type == VDIR) {
2835 error = vn_writechk(vp);
2843 error = VOP_ACCESS(vp, mode, p->p_ucred, td);
2847 if (fmode & O_TRUNC) {
2848 VOP_UNLOCK(vp, 0, td); /* XXX */
2849 VOP_LEASE(vp, td, p->p_ucred, LEASE_WRITE);
2850 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY, td); /* XXX */
2853 error = VOP_SETATTR(vp, vap, p->p_ucred, td);
2857 error = VOP_OPEN(vp, fmode, p->p_ucred, td);
2861 * Make sure that a VM object is created for VMIO support.
2863 if (vn_canvmio(vp) == TRUE) {
2864 if ((error = vfs_object_create(vp, td)) != 0)
2871 * end of vn_open code
2874 if ((error = falloc(p, &nfp, &indx)) != 0) {
2882 * hold an extra reference to avoid having fp ripped out
2883 * from under us while we block in the lock op.
2886 nfp->f_data = (caddr_t)vp;
2887 nfp->f_flag = fmode & FMASK;
2888 nfp->f_ops = &vnops;
2889 nfp->f_type = DTYPE_VNODE;
2890 if (fmode & (O_EXLOCK | O_SHLOCK)) {
2891 lf.l_whence = SEEK_SET;
2894 if (fmode & O_EXLOCK)
2895 lf.l_type = F_WRLCK;
2897 lf.l_type = F_RDLCK;
2899 if ((fmode & FNONBLOCK) == 0)
2901 VOP_UNLOCK(vp, 0, td);
2902 if ((error = VOP_ADVLOCK(vp, (caddr_t)fp, F_SETLK, &lf, type)) != 0) {
2904 * lock request failed. Normally close the descriptor
2905 * but handle the case where someone might have dup()d
2906 * or close()d it when we weren't looking.
2908 if (fdp->fd_ofiles[indx] == fp) {
2909 fdp->fd_ofiles[indx] = NULL;
2914 * release our private reference.
2919 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY, td);
2920 fp->f_flag |= FHASLOCK;
2922 if ((vp->v_type == VREG) && (VOP_GETVOBJECT(vp, NULL) != 0))
2923 vfs_object_create(vp, td);
2925 VOP_UNLOCK(vp, 0, td);
2927 uap->sysmsg_result = indx;
2936 * fhstat_args(struct fhandle *u_fhp, struct stat *sb)
2939 fhstat(struct fhstat_args *uap)
2941 struct thread *td = curthread;
2949 * Must be super user
2955 error = copyin(SCARG(uap, u_fhp), &fh, sizeof(fhandle_t));
2959 if ((mp = vfs_getvfs(&fh.fh_fsid)) == NULL)
2961 if ((error = VFS_FHTOVP(mp, &fh.fh_fid, &vp)))
2963 error = vn_stat(vp, &sb, td);
2967 error = copyout(&sb, SCARG(uap, sb), sizeof(sb));
2972 * fhstatfs_args(struct fhandle *u_fhp, struct statfs *buf)
2975 fhstatfs(struct fhstatfs_args *uap)
2977 struct thread *td = curthread;
2986 * Must be super user
2988 if ((error = suser(td)))
2991 if ((error = copyin(SCARG(uap, u_fhp), &fh, sizeof(fhandle_t))) != 0)
2994 if ((mp = vfs_getvfs(&fh.fh_fsid)) == NULL)
2996 if ((error = VFS_FHTOVP(mp, &fh.fh_fid, &vp)))
3001 if ((error = VFS_STATFS(mp, sp, td)) != 0)
3003 sp->f_flags = mp->mnt_flag & MNT_VISFLAGMASK;
3005 bcopy((caddr_t)sp, (caddr_t)&sb, sizeof(sb));
3006 sb.f_fsid.val[0] = sb.f_fsid.val[1] = 0;
3009 return (copyout(sp, SCARG(uap, buf), sizeof(*sp)));
3013 * Syscall to push extended attribute configuration information into the
3014 * VFS. Accepts a path, which it converts to a mountpoint, as well as
3015 * a command (int cmd), and attribute name and misc data. For now, the
3016 * attribute name is left in userspace for consumption by the VFS_op.
3017 * It will probably be changed to be copied into sysspace by the
3018 * syscall in the future, once issues with various consumers of the
3019 * attribute code have raised their hands.
3021 * Currently this is used only by UFS Extended Attributes.
3024 extattrctl(struct extattrctl_args *uap)
3026 struct thread *td = curthread;
3027 struct nameidata nd;
3031 NDINIT(&nd, NAMEI_LOOKUP, CNP_FOLLOW, UIO_USERSPACE, SCARG(uap, path), td);
3032 if ((error = namei(&nd)) != 0)
3034 mp = nd.ni_vp->v_mount;
3036 return (VFS_EXTATTRCTL(mp, SCARG(uap, cmd), SCARG(uap, attrname),
3037 SCARG(uap, arg), td));
3041 * Syscall to set a named extended attribute on a file or directory.
3042 * Accepts attribute name, and a uio structure pointing to the data to set.
3043 * The uio is consumed in the style of writev(). The real work happens
3044 * in VOP_SETEXTATTR().
3047 extattr_set_file(struct extattr_set_file_args *uap)
3049 struct thread *td = curthread;
3050 struct proc *p = td->td_proc;
3051 struct nameidata nd;
3053 struct iovec *iov, *needfree = NULL, aiov[UIO_SMALLIOV];
3054 char attrname[EXTATTR_MAXNAMELEN];
3058 error = copyin(SCARG(uap, attrname), attrname, EXTATTR_MAXNAMELEN);
3061 NDINIT(&nd, NAMEI_LOOKUP, CNP_FOLLOW | CNP_LOCKLEAF, UIO_USERSPACE,
3062 SCARG(uap, path), td);
3063 if ((error = namei(&nd)) != 0)
3065 iovlen = uap->iovcnt * sizeof(struct iovec);
3066 if (uap->iovcnt > UIO_SMALLIOV) {
3067 if (uap->iovcnt > UIO_MAXIOV) {
3071 MALLOC(iov, struct iovec *, iovlen, M_IOV, M_WAITOK);
3076 auio.uio_iovcnt = uap->iovcnt;
3077 auio.uio_rw = UIO_WRITE;
3078 auio.uio_segflg = UIO_USERSPACE;
3080 auio.uio_offset = 0;
3081 if ((error = copyin((caddr_t)uap->iovp, (caddr_t)iov, iovlen)))
3084 for (i = 0; i < uap->iovcnt; i++) {
3085 if (iov->iov_len > INT_MAX - auio.uio_resid) {
3089 auio.uio_resid += iov->iov_len;
3092 cnt = auio.uio_resid;
3093 error = VOP_SETEXTATTR(nd.ni_vp, attrname, &auio, p->p_ucred, td);
3094 cnt -= auio.uio_resid;
3095 uap->sysmsg_result = cnt;
3098 FREE(needfree, M_IOV);
3104 * Syscall to get a named extended attribute on a file or directory.
3105 * Accepts attribute name, and a uio structure pointing to a buffer for the
3106 * data. The uio is consumed in the style of readv(). The real work
3107 * happens in VOP_GETEXTATTR();
3110 extattr_get_file(struct extattr_get_file_args *uap)
3112 struct thread *td = curthread;
3113 struct proc *p = td->td_proc;
3114 struct nameidata nd;
3116 struct iovec *iov, *needfree, aiov[UIO_SMALLIOV];
3117 char attrname[EXTATTR_MAXNAMELEN];
3121 error = copyin(SCARG(uap, attrname), attrname, EXTATTR_MAXNAMELEN);
3124 NDINIT(&nd, NAMEI_LOOKUP, CNP_FOLLOW | CNP_LOCKLEAF, UIO_USERSPACE,
3125 SCARG(uap, path), td);
3126 if ((error = namei(&nd)) != 0)
3128 iovlen = uap->iovcnt * sizeof (struct iovec);
3129 if (uap->iovcnt > UIO_SMALLIOV) {
3130 if (uap->iovcnt > UIO_MAXIOV) {
3134 MALLOC(iov, struct iovec *, iovlen, M_IOV, M_WAITOK);
3141 auio.uio_iovcnt = uap->iovcnt;
3142 auio.uio_rw = UIO_READ;
3143 auio.uio_segflg = UIO_USERSPACE;
3145 auio.uio_offset = 0;
3146 if ((error = copyin((caddr_t)uap->iovp, (caddr_t)iov, iovlen)))
3149 for (i = 0; i < uap->iovcnt; i++) {
3150 if (iov->iov_len > INT_MAX - auio.uio_resid) {
3154 auio.uio_resid += iov->iov_len;
3157 cnt = auio.uio_resid;
3158 error = VOP_GETEXTATTR(nd.ni_vp, attrname, &auio, p->p_ucred, td);
3159 cnt -= auio.uio_resid;
3160 uap->sysmsg_result = cnt;
3163 FREE(needfree, M_IOV);
3169 * Syscall to delete a named extended attribute from a file or directory.
3170 * Accepts attribute name. The real work happens in VOP_SETEXTATTR().
3173 extattr_delete_file(struct extattr_delete_file_args *uap)
3175 struct thread *td = curthread;
3176 struct proc *p = td->td_proc;
3177 struct nameidata nd;
3178 char attrname[EXTATTR_MAXNAMELEN];
3181 error = copyin(SCARG(uap, attrname), attrname, EXTATTR_MAXNAMELEN);
3184 NDINIT(&nd, NAMEI_LOOKUP, CNP_FOLLOW | CNP_LOCKLEAF, UIO_USERSPACE,
3185 SCARG(uap, path), td);
3186 if ((error = namei(&nd)) != 0)
3188 error = VOP_SETEXTATTR(nd.ni_vp, attrname, NULL, p->p_ucred, td);