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.39 2004/09/28 00:25:29 dillon Exp $
43 #include <sys/param.h>
44 #include <sys/systm.h>
47 #include <sys/sysent.h>
48 #include <sys/malloc.h>
49 #include <sys/mount.h>
50 #include <sys/sysproto.h>
51 #include <sys/filedesc.h>
52 #include <sys/kernel.h>
53 #include <sys/fcntl.h>
55 #include <sys/linker.h>
57 #include <sys/unistd.h>
58 #include <sys/vnode.h>
60 #include <sys/namei.h>
61 #include <sys/dirent.h>
62 #include <sys/extattr.h>
63 #include <sys/kern_syscall.h>
65 #include <machine/limits.h>
66 #include <vfs/union/union.h>
67 #include <sys/sysctl.h>
69 #include <vm/vm_object.h>
70 #include <vm/vm_zone.h>
71 #include <vm/vm_page.h>
73 #include <sys/file2.h>
75 static int checkvp_chdir (struct vnode *vn, struct thread *td);
76 static void checkdirs (struct vnode *olddp);
77 static int chroot_refuse_vdir_fds (struct filedesc *fdp);
78 static int getutimes (const struct timeval *, struct timespec *);
79 static int setfown (struct vnode *, uid_t, gid_t);
80 static int setfmode (struct vnode *, int);
81 static int setfflags (struct vnode *, int);
82 static int setutimes (struct vnode *, const struct timespec *, int);
83 static int usermount = 0; /* if 1, non-root can mount fs. */
85 int (*union_dircheckp) (struct thread *, struct vnode **, struct file *);
87 SYSCTL_INT(_vfs, OID_AUTO, usermount, CTLFLAG_RW, &usermount, 0, "");
90 * Virtual File System System Calls
94 * Mount a file system.
97 * mount_args(char *type, char *path, int flags, caddr_t data)
101 mount(struct mount_args *uap)
103 struct thread *td = curthread;
104 struct proc *p = td->td_proc;
107 struct vfsconf *vfsp;
108 int error, flag = 0, flag2 = 0;
111 char fstypename[MFSNAMELEN];
116 if (p->p_ucred->cr_prison != NULL)
118 if (usermount == 0 && (error = suser(td)))
121 * Do not allow NFS export by non-root users.
123 if (SCARG(uap, flags) & MNT_EXPORTED) {
129 * Silently enforce MNT_NOSUID and MNT_NODEV for non-root users
132 SCARG(uap, flags) |= MNT_NOSUID | MNT_NODEV;
134 * Get vnode to be covered
136 NDINIT(&nd, NAMEI_LOOKUP, CNP_FOLLOW | CNP_LOCKLEAF, UIO_USERSPACE,
137 SCARG(uap, path), td);
138 if ((error = namei(&nd)) != 0)
140 NDFREE(&nd, NDF_ONLY_PNBUF);
142 if (SCARG(uap, flags) & MNT_UPDATE) {
143 if ((vp->v_flag & VROOT) == 0) {
149 flag2 = mp->mnt_kern_flag;
151 * We only allow the filesystem to be reloaded if it
152 * is currently mounted read-only.
154 if ((SCARG(uap, flags) & MNT_RELOAD) &&
155 ((mp->mnt_flag & MNT_RDONLY) == 0)) {
157 return (EOPNOTSUPP); /* Needs translation */
160 * Only root, or the user that did the original mount is
161 * permitted to update it.
163 if (mp->mnt_stat.f_owner != p->p_ucred->cr_uid &&
164 (error = suser(td))) {
168 if (vfs_busy(mp, LK_NOWAIT, NULL, td)) {
172 lwkt_gettoken(&vlock, vp->v_interlock);
173 if ((vp->v_flag & VMOUNT) != 0 ||
174 vp->v_mountedhere != NULL) {
175 lwkt_reltoken(&vlock);
180 vp->v_flag |= VMOUNT;
181 lwkt_reltoken(&vlock);
183 SCARG(uap, flags) & (MNT_RELOAD | MNT_FORCE | MNT_UPDATE);
184 VOP_UNLOCK(vp, NULL, 0, td);
188 * If the user is not root, ensure that they own the directory
189 * onto which we are attempting to mount.
191 if ((error = VOP_GETATTR(vp, &va, td)) ||
192 (va.va_uid != p->p_ucred->cr_uid &&
193 (error = suser(td)))) {
197 if ((error = vinvalbuf(vp, V_SAVE, td, 0, 0)) != 0) {
201 if (vp->v_type != VDIR) {
205 if ((error = copyinstr(SCARG(uap, type), fstypename, MFSNAMELEN, NULL)) != 0) {
209 for (vfsp = vfsconf; vfsp; vfsp = vfsp->vfc_next)
210 if (!strcmp(vfsp->vfc_name, fstypename))
215 /* Only load modules for root (very important!) */
216 if ((error = suser(td)) != 0) {
220 error = linker_load_file(fstypename, &lf);
221 if (error || lf == NULL) {
228 /* lookup again, see if the VFS was loaded */
229 for (vfsp = vfsconf; vfsp; vfsp = vfsp->vfc_next)
230 if (!strcmp(vfsp->vfc_name, fstypename))
234 linker_file_unload(lf);
239 lwkt_gettoken(&vlock, vp->v_interlock);
240 if ((vp->v_flag & VMOUNT) != 0 ||
241 vp->v_mountedhere != NULL) {
242 lwkt_reltoken(&vlock);
246 vp->v_flag |= VMOUNT;
247 lwkt_reltoken(&vlock);
250 * Allocate and initialize the filesystem.
252 mp = malloc(sizeof(struct mount), M_MOUNT, M_ZERO|M_WAITOK);
253 TAILQ_INIT(&mp->mnt_nvnodelist);
254 TAILQ_INIT(&mp->mnt_reservedvnlist);
255 mp->mnt_nvnodelistsize = 0;
256 lockinit(&mp->mnt_lock, 0, "vfslock", 0, LK_NOPAUSE);
257 vfs_busy(mp, LK_NOWAIT, NULL, td);
258 mp->mnt_op = vfsp->vfc_vfsops;
260 vfsp->vfc_refcount++;
261 mp->mnt_stat.f_type = vfsp->vfc_typenum;
262 mp->mnt_flag |= vfsp->vfc_flags & MNT_VISFLAGMASK;
263 strncpy(mp->mnt_stat.f_fstypename, vfsp->vfc_name, MFSNAMELEN);
264 mp->mnt_vnodecovered = vp;
265 mp->mnt_stat.f_owner = p->p_ucred->cr_uid;
266 mp->mnt_iosize_max = DFLTPHYS;
267 VOP_UNLOCK(vp, NULL, 0, td);
270 * Set the mount level flags.
272 if (SCARG(uap, flags) & MNT_RDONLY)
273 mp->mnt_flag |= MNT_RDONLY;
274 else if (mp->mnt_flag & MNT_RDONLY)
275 mp->mnt_kern_flag |= MNTK_WANTRDWR;
276 mp->mnt_flag &=~ (MNT_NOSUID | MNT_NOEXEC | MNT_NODEV |
277 MNT_SYNCHRONOUS | MNT_UNION | MNT_ASYNC | MNT_NOATIME |
278 MNT_NOSYMFOLLOW | MNT_IGNORE |
279 MNT_NOCLUSTERR | MNT_NOCLUSTERW | MNT_SUIDDIR);
280 mp->mnt_flag |= SCARG(uap, flags) & (MNT_NOSUID | MNT_NOEXEC |
281 MNT_NODEV | MNT_SYNCHRONOUS | MNT_UNION | MNT_ASYNC | MNT_FORCE |
282 MNT_NOSYMFOLLOW | MNT_IGNORE |
283 MNT_NOATIME | MNT_NOCLUSTERR | MNT_NOCLUSTERW | MNT_SUIDDIR);
285 * Mount the filesystem.
286 * XXX The final recipients of VFS_MOUNT just overwrite the ndp they
287 * get. No freeing of cn_pnbuf.
289 error = VFS_MOUNT(mp, SCARG(uap, path), SCARG(uap, data), &nd, td);
290 if (mp->mnt_flag & MNT_UPDATE) {
291 if (mp->mnt_kern_flag & MNTK_WANTRDWR)
292 mp->mnt_flag &= ~MNT_RDONLY;
293 mp->mnt_flag &=~ (MNT_UPDATE | MNT_RELOAD | MNT_FORCE);
294 mp->mnt_kern_flag &=~ MNTK_WANTRDWR;
297 mp->mnt_kern_flag = flag2;
300 lwkt_gettoken(&vlock, vp->v_interlock);
301 vp->v_flag &= ~VMOUNT;
302 lwkt_reltoken(&vlock);
306 vn_lock(vp, NULL, LK_EXCLUSIVE | LK_RETRY, td);
308 * Put the new filesystem on the mount list after root.
312 lwkt_gettoken(&vlock, vp->v_interlock);
313 vp->v_flag &= ~VMOUNT;
314 vp->v_mountedhere = mp;
315 lwkt_reltoken(&vlock);
316 lwkt_gettoken(&ilock, &mountlist_token);
317 TAILQ_INSERT_TAIL(&mountlist, mp, mnt_list);
318 lwkt_reltoken(&ilock);
320 VOP_UNLOCK(vp, NULL, 0, td);
321 error = vfs_allocate_syncvnode(mp);
323 if ((error = VFS_START(mp, 0, td)) != 0)
326 vfs_rm_vnodeops(&mp->mnt_vn_ops);
327 vfs_rm_vnodeops(&mp->mnt_vn_spec_ops);
328 vfs_rm_vnodeops(&mp->mnt_vn_fifo_ops);
329 lwkt_gettoken(&vlock, vp->v_interlock);
330 vp->v_flag &= ~VMOUNT;
331 lwkt_reltoken(&vlock);
332 mp->mnt_vfc->vfc_refcount--;
341 * Scan all active processes to see if any of them have a current
342 * or root directory onto which the new filesystem has just been
343 * mounted. If so, replace them with the new mount point.
346 checkdirs(struct vnode *olddp)
348 struct filedesc *fdp;
353 if (olddp->v_usecount == 1)
355 mp = olddp->v_mountedhere;
356 if (VFS_ROOT(mp, &newdp))
357 panic("mount: lost mount");
358 FOREACH_PROC_IN_SYSTEM(p) {
360 if (fdp->fd_cdir == olddp) {
363 fdp->fd_cdir = newdp;
364 cache_drop(fdp->fd_ncdir);
365 fdp->fd_ncdir = cache_vptoncp(newdp);
367 if (fdp->fd_rdir == olddp) {
370 fdp->fd_rdir = newdp;
371 cache_drop(fdp->fd_nrdir);
372 fdp->fd_nrdir = cache_vptoncp(newdp);
375 if (rootvnode == olddp) {
379 vfs_cache_setroot(rootvnode);
385 * Unmount a file system.
387 * Note: unmount takes a path to the vnode mounted on as argument,
388 * not special file (as before).
391 * umount_args(char *path, int flags)
395 unmount(struct unmount_args *uap)
397 struct thread *td = curthread;
398 struct proc *p = td->td_proc;
405 if (p->p_ucred->cr_prison != NULL)
407 if (usermount == 0 && (error = suser(td)))
410 NDINIT(&nd, NAMEI_LOOKUP, CNP_FOLLOW | CNP_LOCKLEAF, UIO_USERSPACE,
411 SCARG(uap, path), td);
412 if ((error = namei(&nd)) != 0)
415 NDFREE(&nd, NDF_ONLY_PNBUF);
419 * Only root, or the user that did the original mount is
420 * permitted to unmount this filesystem.
422 if ((mp->mnt_stat.f_owner != p->p_ucred->cr_uid) &&
423 (error = suser(td))) {
429 * Don't allow unmounting the root file system.
431 if (mp->mnt_flag & MNT_ROOTFS) {
437 * Must be the root of the filesystem
439 if ((vp->v_flag & VROOT) == 0) {
444 return (dounmount(mp, SCARG(uap, flags), td));
448 * Do the actual file system unmount.
451 dounmount(struct mount *mp, int flags, struct thread *td)
453 struct vnode *coveredvp;
458 lwkt_gettoken(&ilock, &mountlist_token);
459 if (mp->mnt_kern_flag & MNTK_UNMOUNT) {
460 lwkt_reltoken(&ilock);
463 mp->mnt_kern_flag |= MNTK_UNMOUNT;
464 /* Allow filesystems to detect that a forced unmount is in progress. */
465 if (flags & MNT_FORCE)
466 mp->mnt_kern_flag |= MNTK_UNMOUNTF;
467 error = lockmgr(&mp->mnt_lock, LK_DRAIN | LK_INTERLOCK |
468 ((flags & MNT_FORCE) ? 0 : LK_NOWAIT), &ilock, td);
470 mp->mnt_kern_flag &= ~(MNTK_UNMOUNT | MNTK_UNMOUNTF);
471 if (mp->mnt_kern_flag & MNTK_MWAIT)
476 if (mp->mnt_flag & MNT_EXPUBLIC)
477 vfs_setpublicfs(NULL, NULL, NULL);
479 vfs_msync(mp, MNT_WAIT);
480 async_flag = mp->mnt_flag & MNT_ASYNC;
481 mp->mnt_flag &=~ MNT_ASYNC;
482 cache_purgevfs(mp); /* remove cache entries for this file sys */
483 if (mp->mnt_syncer != NULL)
484 vrele(mp->mnt_syncer);
485 if (((mp->mnt_flag & MNT_RDONLY) ||
486 (error = VFS_SYNC(mp, MNT_WAIT, td)) == 0) ||
488 error = VFS_UNMOUNT(mp, flags, td);
489 lwkt_gettokref(&ilock);
491 if (mp->mnt_syncer == NULL)
492 vfs_allocate_syncvnode(mp);
493 mp->mnt_kern_flag &= ~(MNTK_UNMOUNT | MNTK_UNMOUNTF);
494 mp->mnt_flag |= async_flag;
495 lockmgr(&mp->mnt_lock, LK_RELEASE | LK_INTERLOCK | LK_REENABLE,
497 if (mp->mnt_kern_flag & MNTK_MWAIT)
501 TAILQ_REMOVE(&mountlist, mp, mnt_list);
504 * Remove any installed vnode ops here so the individual VFSs don't
507 vfs_rm_vnodeops(&mp->mnt_vn_ops);
508 vfs_rm_vnodeops(&mp->mnt_vn_spec_ops);
509 vfs_rm_vnodeops(&mp->mnt_vn_fifo_ops);
511 if ((coveredvp = mp->mnt_vnodecovered) != NULLVP) {
512 coveredvp->v_mountedhere = NULL;
515 mp->mnt_vfc->vfc_refcount--;
516 if (!TAILQ_EMPTY(&mp->mnt_nvnodelist))
517 panic("unmount: dangling vnode");
518 lockmgr(&mp->mnt_lock, LK_RELEASE | LK_INTERLOCK, &ilock, td);
519 if (mp->mnt_kern_flag & MNTK_MWAIT)
526 * Sync each mounted filesystem.
530 static int syncprt = 0;
531 SYSCTL_INT(_debug, OID_AUTO, syncprt, CTLFLAG_RW, &syncprt, 0, "");
536 sync(struct sync_args *uap)
538 struct thread *td = curthread;
539 struct mount *mp, *nmp;
543 lwkt_gettoken(&ilock, &mountlist_token);
544 for (mp = TAILQ_FIRST(&mountlist); mp != NULL; mp = nmp) {
545 if (vfs_busy(mp, LK_NOWAIT, &ilock, td)) {
546 nmp = TAILQ_NEXT(mp, mnt_list);
549 if ((mp->mnt_flag & MNT_RDONLY) == 0) {
550 asyncflag = mp->mnt_flag & MNT_ASYNC;
551 mp->mnt_flag &= ~MNT_ASYNC;
552 vfs_msync(mp, MNT_NOWAIT);
553 VFS_SYNC(mp, MNT_NOWAIT, td);
554 mp->mnt_flag |= asyncflag;
556 lwkt_gettokref(&ilock);
557 nmp = TAILQ_NEXT(mp, mnt_list);
560 lwkt_reltoken(&ilock);
562 * print out buffer pool stat information on each sync() call.
571 /* XXX PRISON: could be per prison flag */
572 static int prison_quotas;
574 SYSCTL_INT(_kern_prison, OID_AUTO, quotas, CTLFLAG_RW, &prison_quotas, 0, "");
578 * quotactl_args(char *path, int fcmd, int uid, caddr_t arg)
580 * Change filesystem quotas.
584 quotactl(struct quotactl_args *uap)
586 struct thread *td = curthread;
587 struct proc *p = td->td_proc;
593 if (p->p_ucred->cr_prison && !prison_quotas)
595 NDINIT(&nd, NAMEI_LOOKUP, CNP_FOLLOW, UIO_USERSPACE,
596 SCARG(uap, path), td);
597 if ((error = namei(&nd)) != 0)
599 mp = nd.ni_vp->v_mount;
600 NDFREE(&nd, NDF_ONLY_PNBUF);
602 return (VFS_QUOTACTL(mp, SCARG(uap, cmd), SCARG(uap, uid),
603 SCARG(uap, arg), td));
607 kern_statfs(struct nameidata *nd, struct statfs *buf)
609 struct thread *td = curthread;
617 mp = nd->ni_vp->v_mount;
619 NDFREE(nd, NDF_ONLY_PNBUF);
621 error = VFS_STATFS(mp, sp, td);
624 sp->f_flags = mp->mnt_flag & MNT_VISFLAGMASK;
625 bcopy(sp, buf, sizeof(*buf));
626 /* Only root should have access to the fsid's. */
628 buf->f_fsid.val[0] = buf->f_fsid.val[1] = 0;
633 * statfs_args(char *path, struct statfs *buf)
635 * Get filesystem statistics.
638 statfs(struct statfs_args *uap)
640 struct thread *td = curthread;
645 NDINIT(&nd, NAMEI_LOOKUP, CNP_FOLLOW, UIO_USERSPACE, uap->path, td);
647 error = kern_statfs(&nd, &buf);
650 error = copyout(&buf, uap->buf, sizeof(*uap->buf));
655 kern_fstatfs(int fd, struct statfs *buf)
657 struct thread *td = curthread;
658 struct proc *p = td->td_proc;
665 error = getvnode(p->p_fd, fd, &fp);
668 mp = ((struct vnode *)fp->f_data)->v_mount;
672 error = VFS_STATFS(mp, sp, td);
675 sp->f_flags = mp->mnt_flag & MNT_VISFLAGMASK;
676 bcopy(sp, buf, sizeof(*buf));
677 /* Only root should have access to the fsid's. */
679 buf->f_fsid.val[0] = buf->f_fsid.val[1] = 0;
684 * fstatfs_args(int fd, struct statfs *buf)
686 * Get filesystem statistics.
689 fstatfs(struct fstatfs_args *uap)
694 error = kern_fstatfs(uap->fd, &buf);
697 error = copyout(&buf, uap->buf, sizeof(*uap->buf));
702 * getfsstat_args(struct statfs *buf, long bufsize, int flags)
704 * Get statistics on all filesystems.
708 getfsstat(struct getfsstat_args *uap)
710 struct thread *td = curthread;
711 struct mount *mp, *nmp;
715 long count, maxcount, error;
717 maxcount = SCARG(uap, bufsize) / sizeof(struct statfs);
718 sfsp = (caddr_t)SCARG(uap, buf);
720 lwkt_gettoken(&ilock, &mountlist_token);
721 for (mp = TAILQ_FIRST(&mountlist); mp != NULL; mp = nmp) {
722 if (vfs_busy(mp, LK_NOWAIT, &ilock, td)) {
723 nmp = TAILQ_NEXT(mp, mnt_list);
726 if (sfsp && count < maxcount) {
729 * If MNT_NOWAIT or MNT_LAZY is specified, do not
730 * refresh the fsstat cache. MNT_NOWAIT or MNT_LAZY
731 * overrides MNT_WAIT.
733 if (((SCARG(uap, flags) & (MNT_LAZY|MNT_NOWAIT)) == 0 ||
734 (SCARG(uap, flags) & MNT_WAIT)) &&
735 (error = VFS_STATFS(mp, sp, td))) {
736 lwkt_gettokref(&ilock);
737 nmp = TAILQ_NEXT(mp, mnt_list);
741 sp->f_flags = mp->mnt_flag & MNT_VISFLAGMASK;
742 error = copyout(sp, sfsp, sizeof(*sp));
750 lwkt_gettokref(&ilock);
751 nmp = TAILQ_NEXT(mp, mnt_list);
754 lwkt_reltoken(&ilock);
755 if (sfsp && count > maxcount)
756 uap->sysmsg_result = maxcount;
758 uap->sysmsg_result = count;
763 * fchdir_args(int fd)
765 * Change current working directory to a given file descriptor.
769 fchdir(struct fchdir_args *uap)
771 struct thread *td = curthread;
772 struct proc *p = td->td_proc;
773 struct filedesc *fdp = p->p_fd;
774 struct vnode *vp, *tdp;
779 if ((error = getvnode(fdp, SCARG(uap, fd), &fp)) != 0)
781 vp = (struct vnode *)fp->f_data;
783 vn_lock(vp, NULL, LK_EXCLUSIVE | LK_RETRY, td);
784 if (vp->v_type != VDIR)
787 error = VOP_ACCESS(vp, VEXEC, p->p_ucred, td);
788 while (!error && (mp = vp->v_mountedhere) != NULL) {
789 if (vfs_busy(mp, 0, NULL, td))
791 error = VFS_ROOT(mp, &tdp);
802 VOP_UNLOCK(vp, NULL, 0, td);
805 fdp->fd_ncdir = cache_vptoncp(vp);
810 kern_chdir(struct nameidata *nd)
812 struct thread *td = curthread;
813 struct proc *p = td->td_proc;
814 struct filedesc *fdp = p->p_fd;
817 if ((error = namei(nd)) != 0)
819 if ((error = checkvp_chdir(nd->ni_vp, td)) == 0) {
821 cache_drop(fdp->fd_ncdir);
822 fdp->fd_cdir = nd->ni_vp;
823 fdp->fd_ncdir = cache_vptoncp(nd->ni_vp); /* stopgap */
826 NDFREE(nd, ~(NDF_NO_FREE_PNBUF | NDF_NO_VP_PUT));
831 * chdir_args(char *path)
833 * Change current working directory (``.'').
836 chdir(struct chdir_args *uap)
838 struct thread *td = curthread;
842 NDINIT(&nd, NAMEI_LOOKUP, CNP_FOLLOW | CNP_LOCKLEAF, UIO_USERSPACE,
845 error = kern_chdir(&nd);
851 * Helper function for raised chroot(2) security function: Refuse if
852 * any filedescriptors are open directories.
855 chroot_refuse_vdir_fds(fdp)
856 struct filedesc *fdp;
863 for (fd = 0; fd < fdp->fd_nfiles ; fd++) {
864 error = getvnode(fdp, fd, &fp);
867 vp = (struct vnode *)fp->f_data;
868 if (vp->v_type != VDIR)
876 * This sysctl determines if we will allow a process to chroot(2) if it
877 * has a directory open:
878 * 0: disallowed for all processes.
879 * 1: allowed for processes that were not already chroot(2)'ed.
880 * 2: allowed for all processes.
883 static int chroot_allow_open_directories = 1;
885 SYSCTL_INT(_kern, OID_AUTO, chroot_allow_open_directories, CTLFLAG_RW,
886 &chroot_allow_open_directories, 0, "");
889 * Chroot to the specified vnode. vp must be locked and referenced on
890 * call, and will be left locked and referenced on return. This routine
891 * may acquire additional refs on the vnode when associating it with
892 * the process's root and/or jail dirs.
895 kern_chroot(struct vnode *vp)
897 struct thread *td = curthread;
898 struct proc *p = td->td_proc;
899 struct filedesc *fdp = p->p_fd;
903 * Only root can chroot
905 if ((error = suser_cred(p->p_ucred, PRISON_ROOT)) != 0)
909 * Disallow open directory descriptors (fchdir() breakouts).
911 if (chroot_allow_open_directories == 0 ||
912 (chroot_allow_open_directories == 1 && fdp->fd_rdir != rootvnode)) {
913 if ((error = chroot_refuse_vdir_fds(fdp)) != 0)
918 * Check the validity of vp as a directory to change to and
919 * associate it with rdir/jdir.
921 if ((error = checkvp_chdir(vp, td)) == 0) {
923 cache_drop(fdp->fd_nrdir);
926 fdp->fd_nrdir = cache_vptoncp(vp);
927 if (fdp->fd_jdir == NULL) {
930 fdp->fd_njdir = cache_vptoncp(vp);
937 * chroot_args(char *path)
939 * Change notion of root (``/'') directory.
943 chroot(struct chroot_args *uap)
945 struct thread *td = curthread;
949 KKASSERT(td->td_proc);
950 NDINIT(&nd, NAMEI_LOOKUP, CNP_FOLLOW | CNP_LOCKLEAF, UIO_USERSPACE,
951 SCARG(uap, path), td);
952 if ((error = namei(&nd)) == 0) {
953 error = kern_chroot(nd.ni_vp);
954 NDFREE(&nd, ~(NDF_NO_FREE_PNBUF | NDF_NO_VP_PUT));
960 * Common routine for chroot and chdir. Given a locked, referenced vnode,
961 * determine whether it is legal to chdir to the vnode. The vnode's state
962 * is not changed by this call.
965 checkvp_chdir(struct vnode *vp, struct thread *td)
969 if (vp->v_type != VDIR)
972 error = VOP_ACCESS(vp, VEXEC, td->td_proc->p_ucred, td);
977 kern_open(struct nameidata *nd, int oflags, int mode, int *res)
979 struct thread *td = curthread;
980 struct proc *p = td->td_proc;
981 struct filedesc *fdp = p->p_fd;
986 int type, indx, error;
989 if ((oflags & O_ACCMODE) == O_ACCMODE)
991 flags = FFLAGS(oflags);
992 error = falloc(p, &nfp, &indx);
996 cmode = ((mode &~ fdp->fd_cmask) & ALLPERMS) &~ S_ISTXT;
997 p->p_dupfd = -indx - 1; /* XXX check for fdopen */
999 * Bump the ref count to prevent another process from closing
1000 * the descriptor while we are blocked in vn_open()
1003 error = vn_open(nd, flags, cmode);
1006 * release our own reference
1011 * handle special fdopen() case. bleh. dupfdopen() is
1012 * responsible for dropping the old contents of ofiles[indx]
1015 if ((error == ENODEV || error == ENXIO) &&
1016 p->p_dupfd >= 0 && /* XXX from fdopen */
1018 dupfdopen(fdp, indx, p->p_dupfd, flags, error)) == 0) {
1023 * Clean up the descriptor, but only if another thread hadn't
1024 * replaced or closed it.
1026 if (fdp->fd_ofiles[indx] == fp) {
1027 fdp->fd_ofiles[indx] = NULL;
1031 if (error == ERESTART)
1036 NDFREE(nd, NDF_ONLY_PNBUF);
1040 * There should be 2 references on the file, one from the descriptor
1041 * table, and one for us.
1043 * Handle the case where someone closed the file (via its file
1044 * descriptor) while we were blocked. The end result should look
1045 * like opening the file succeeded but it was immediately closed.
1047 if (fp->f_count == 1) {
1048 KASSERT(fdp->fd_ofiles[indx] != fp,
1049 ("Open file descriptor lost all refs"));
1050 VOP_UNLOCK(vp, NULL, 0, td);
1051 vn_close(vp, flags & FMASK, td);
1057 fp->f_data = (caddr_t)vp;
1058 fp->f_flag = flags & FMASK;
1060 fp->f_type = (vp->v_type == VFIFO ? DTYPE_FIFO : DTYPE_VNODE);
1061 if (flags & (O_EXLOCK | O_SHLOCK)) {
1062 lf.l_whence = SEEK_SET;
1065 if (flags & O_EXLOCK)
1066 lf.l_type = F_WRLCK;
1068 lf.l_type = F_RDLCK;
1070 if ((flags & FNONBLOCK) == 0)
1072 VOP_UNLOCK(vp, NULL, 0, td);
1073 if ((error = VOP_ADVLOCK(vp, (caddr_t)fp, F_SETLK, &lf, type)) != 0) {
1075 * lock request failed. Normally close the descriptor
1076 * but handle the case where someone might have dup()d
1077 * it when we weren't looking. One reference is
1078 * owned by the descriptor array, the other by us.
1080 if (fdp->fd_ofiles[indx] == fp) {
1081 fdp->fd_ofiles[indx] = NULL;
1087 vn_lock(vp, NULL, LK_EXCLUSIVE | LK_RETRY, td);
1088 fp->f_flag |= FHASLOCK;
1090 /* assert that vn_open created a backing object if one is needed */
1091 KASSERT(!vn_canvmio(vp) || VOP_GETVOBJECT(vp, NULL) == 0,
1092 ("open: vmio vnode has no backing object after vn_open"));
1093 VOP_UNLOCK(vp, NULL, 0, td);
1096 * release our private reference, leaving the one associated with the
1097 * descriptor table intact.
1105 * open_args(char *path, int flags, int mode)
1107 * Check permissions, allocate an open file structure,
1108 * and call the device open routine if any.
1111 open(struct open_args *uap)
1113 struct thread *td = curthread;
1114 struct nameidata nd;
1117 NDINIT(&nd, NAMEI_LOOKUP, CNP_FOLLOW, UIO_USERSPACE, uap->path, td);
1119 error = kern_open(&nd, uap->flags, uap->mode, &uap->sysmsg_result);
1125 kern_mknod(struct nameidata *nd, int mode, int dev)
1127 struct thread *td = curthread;
1128 struct proc *p = td->td_proc;
1136 switch (mode & S_IFMT) {
1142 error = suser_cred(p->p_ucred, PRISON_ROOT);
1156 vattr.va_mode = (mode & ALLPERMS) &~ p->p_fd->fd_cmask;
1157 vattr.va_rdev = dev;
1160 switch (mode & S_IFMT) {
1161 case S_IFMT: /* used by badsect to flag bad sectors */
1162 vattr.va_type = VBAD;
1165 vattr.va_type = VCHR;
1168 vattr.va_type = VBLK;
1179 VOP_LEASE(nd->ni_dvp, td, p->p_ucred, LEASE_WRITE);
1181 error = VOP_WHITEOUT(nd->ni_dvp, NCPNULL,
1182 &nd->ni_cnd, NAMEI_CREATE);
1184 error = VOP_MKNOD(nd->ni_dvp, NCPNULL, &nd->ni_vp,
1185 &nd->ni_cnd, &vattr);
1189 NDFREE(nd, NDF_ONLY_PNBUF);
1192 NDFREE(nd, NDF_ONLY_PNBUF);
1193 if (nd->ni_dvp == vp)
1200 ASSERT_VOP_UNLOCKED(nd->ni_dvp, "mknod");
1201 ASSERT_VOP_UNLOCKED(nd->ni_vp, "mknod");
1206 * mknod_args(char *path, int mode, int dev)
1208 * Create a special file.
1211 mknod(struct mknod_args *uap)
1213 struct thread *td = curthread;
1214 struct nameidata nd;
1217 NDINIT(&nd, NAMEI_CREATE, CNP_LOCKPARENT, UIO_USERSPACE, uap->path,
1220 error = kern_mknod(&nd, uap->mode, uap->dev);
1226 kern_mkfifo(struct nameidata *nd, int mode)
1228 struct thread *td = curthread;
1229 struct proc *p = td->td_proc;
1237 if (nd->ni_vp != NULL) {
1238 NDFREE(nd, NDF_ONLY_PNBUF);
1239 if (nd->ni_dvp == nd->ni_vp)
1247 vattr.va_type = VFIFO;
1248 vattr.va_mode = (mode & ALLPERMS) &~ p->p_fd->fd_cmask;
1249 VOP_LEASE(nd->ni_dvp, td, p->p_ucred, LEASE_WRITE);
1250 error = VOP_MKNOD(nd->ni_dvp, NCPNULL, &nd->ni_vp, &nd->ni_cnd, &vattr);
1253 NDFREE(nd, NDF_ONLY_PNBUF);
1259 * mkfifo_args(char *path, int mode)
1261 * Create a named pipe.
1264 mkfifo(struct mkfifo_args *uap)
1266 struct thread *td = curthread;
1267 struct nameidata nd;
1270 NDINIT(&nd, NAMEI_CREATE, CNP_LOCKPARENT, UIO_USERSPACE, uap->path,
1273 error = kern_mkfifo(&nd, uap->mode);
1279 kern_link(struct nameidata *nd, struct nameidata *linknd)
1281 struct thread *td = curthread;
1282 struct proc *p = td->td_proc;
1290 NDFREE(nd, NDF_ONLY_PNBUF);
1292 if (vp->v_type == VDIR)
1293 error = EPERM; /* POSIX */
1295 error = namei(linknd);
1297 if (linknd->ni_vp != NULL) {
1299 vrele(linknd->ni_vp);
1302 VOP_LEASE(linknd->ni_dvp, td, p->p_ucred,
1304 VOP_LEASE(vp, td, p->p_ucred, LEASE_WRITE);
1305 error = VOP_LINK(linknd->ni_dvp, NCPNULL, vp,
1308 NDFREE(linknd, NDF_ONLY_PNBUF);
1309 if (linknd->ni_dvp == linknd->ni_vp)
1310 vrele(linknd->ni_dvp);
1312 vput(linknd->ni_dvp);
1313 ASSERT_VOP_UNLOCKED(linknd->ni_dvp, "link");
1314 ASSERT_VOP_UNLOCKED(linknd->ni_vp, "link");
1322 * link_args(char *path, char *link)
1324 * Make a hard file link.
1327 link(struct link_args *uap)
1329 struct thread *td = curthread;
1330 struct nameidata nd, linknd;
1333 NDINIT(&nd, NAMEI_LOOKUP, CNP_FOLLOW | CNP_NOOBJ, UIO_USERSPACE,
1335 NDINIT(&linknd, NAMEI_CREATE, CNP_LOCKPARENT | CNP_NOOBJ,
1336 UIO_USERSPACE, uap->link, td);
1338 error = kern_link(&nd, &linknd);
1344 kern_symlink(char *path, struct nameidata *nd)
1346 struct thread *td = curthread;
1347 struct proc *p = td->td_proc;
1356 NDFREE(nd, NDF_ONLY_PNBUF);
1357 if (nd->ni_dvp == nd->ni_vp)
1365 vattr.va_mode = ACCESSPERMS &~ p->p_fd->fd_cmask;
1366 VOP_LEASE(nd->ni_dvp, td, p->p_ucred, LEASE_WRITE);
1367 error = VOP_SYMLINK(nd->ni_dvp, NCPNULL, &nd->ni_vp, &nd->ni_cnd,
1369 NDFREE(nd, NDF_ONLY_PNBUF);
1373 ASSERT_VOP_UNLOCKED(nd->ni_dvp, "symlink");
1374 ASSERT_VOP_UNLOCKED(nd->ni_vp, "symlink");
1380 * symlink(char *path, char *link)
1382 * Make a symbolic link.
1385 symlink(struct symlink_args *uap)
1387 struct thread *td = curthread;
1388 struct nameidata nd;
1392 path = zalloc(namei_zone);
1393 error = copyinstr(uap->path, path, MAXPATHLEN, NULL);
1395 NDINIT(&nd, NAMEI_CREATE, CNP_LOCKPARENT | CNP_NOOBJ,
1396 UIO_USERSPACE, uap->link, td);
1397 error = kern_symlink(path, &nd);
1399 zfree(namei_zone, path);
1404 * undelete_args(char *path)
1406 * Delete a whiteout from the filesystem.
1410 undelete(struct undelete_args *uap)
1412 struct thread *td = curthread;
1413 struct proc *p = td->td_proc;
1415 struct nameidata nd;
1418 NDINIT(&nd, NAMEI_DELETE, CNP_LOCKPARENT | CNP_DOWHITEOUT, UIO_USERSPACE,
1419 SCARG(uap, path), td);
1424 if (nd.ni_vp != NULLVP || !(nd.ni_cnd.cn_flags & CNP_ISWHITEOUT)) {
1425 NDFREE(&nd, NDF_ONLY_PNBUF);
1426 if (nd.ni_dvp == nd.ni_vp)
1435 VOP_LEASE(nd.ni_dvp, td, p->p_ucred, LEASE_WRITE);
1436 error = VOP_WHITEOUT(nd.ni_dvp, NCPNULL, &nd.ni_cnd, NAMEI_DELETE);
1437 NDFREE(&nd, NDF_ONLY_PNBUF);
1439 ASSERT_VOP_UNLOCKED(nd.ni_dvp, "undelete");
1440 ASSERT_VOP_UNLOCKED(nd.ni_vp, "undelete");
1445 kern_unlink(struct nameidata *nd)
1447 struct thread *td = curthread;
1448 struct proc *p = td->td_proc;
1457 VOP_LEASE(vp, td, p->p_ucred, LEASE_WRITE);
1458 vn_lock(vp, NULL, LK_EXCLUSIVE | LK_RETRY, td);
1460 if (vp->v_type == VDIR)
1461 error = EPERM; /* POSIX */
1464 * The root of a mounted filesystem cannot be deleted.
1466 * XXX: can this only be a VDIR case?
1468 if (vp->v_flag & VROOT)
1473 VOP_LEASE(nd->ni_dvp, td, p->p_ucred, LEASE_WRITE);
1474 error = VOP_REMOVE(nd->ni_dvp, NCPNULL, vp, &nd->ni_cnd);
1476 NDFREE(nd, NDF_ONLY_PNBUF);
1477 if (nd->ni_dvp == vp)
1483 ASSERT_VOP_UNLOCKED(nd->ni_dvp, "unlink");
1484 ASSERT_VOP_UNLOCKED(nd->ni_vp, "unlink");
1489 * unlink_args(char *path)
1491 * Delete a name from the filesystem.
1494 unlink(struct unlink_args *uap)
1496 struct thread *td = curthread;
1497 struct nameidata nd;
1500 NDINIT(&nd, NAMEI_DELETE, CNP_LOCKPARENT, UIO_USERSPACE, uap->path,
1503 error = kern_unlink(&nd);
1509 kern_lseek(int fd, off_t offset, int whence, off_t *res)
1511 struct thread *td = curthread;
1512 struct proc *p = td->td_proc;
1513 struct filedesc *fdp = p->p_fd;
1518 if (fd >= fdp->fd_nfiles ||
1519 (fp = fdp->fd_ofiles[fd]) == NULL)
1521 if (fp->f_type != DTYPE_VNODE)
1525 fp->f_offset += offset;
1528 error=VOP_GETATTR((struct vnode *)fp->f_data, &vattr, td);
1531 fp->f_offset = offset + vattr.va_size;
1534 fp->f_offset = offset;
1539 *res = fp->f_offset;
1544 * lseek_args(int fd, int pad, off_t offset, int whence)
1546 * Reposition read/write file offset.
1549 lseek(struct lseek_args *uap)
1553 error = kern_lseek(uap->fd, uap->offset, uap->whence,
1554 &uap->sysmsg_offset);
1560 kern_access(struct nameidata *nd, int aflags)
1562 struct thread *td = curthread;
1563 struct proc *p = td->td_proc;
1564 struct ucred *cred, *tmpcred;
1570 * Create and modify a temporary credential instead of one that
1571 * is potentially shared. This could also mess up socket
1572 * buffer accounting which can run in an interrupt context.
1574 tmpcred = crdup(cred);
1575 tmpcred->cr_uid = p->p_ucred->cr_ruid;
1576 tmpcred->cr_groups[0] = p->p_ucred->cr_rgid;
1577 p->p_ucred = tmpcred;
1578 nd->ni_cnd.cn_cred = tmpcred;
1584 /* Flags == 0 means only check for existence. */
1593 if ((flags & VWRITE) == 0 || (error = vn_writechk(vp)) == 0)
1594 error = VOP_ACCESS(vp, flags, tmpcred, td);
1596 NDFREE(nd, NDF_ONLY_PNBUF);
1605 * access_args(char *path, int flags)
1607 * Check access permissions.
1610 access(struct access_args *uap)
1612 struct thread *td = curthread;
1613 struct nameidata nd;
1616 NDINIT(&nd, NAMEI_LOOKUP, CNP_FOLLOW | CNP_LOCKLEAF | CNP_NOOBJ,
1617 UIO_USERSPACE, uap->path, td);
1619 error = kern_access(&nd, uap->flags);
1625 kern_stat(struct nameidata *nd, struct stat *st)
1627 struct thread *td = curthread;
1633 error = vn_stat(nd->ni_vp, st, td);
1634 NDFREE(nd, NDF_ONLY_PNBUF);
1640 * stat_args(char *path, struct stat *ub)
1642 * Get file status; this version follows links.
1645 stat(struct stat_args *uap)
1647 struct thread *td = curthread;
1648 struct nameidata nd;
1652 NDINIT(&nd, NAMEI_LOOKUP, CNP_FOLLOW | CNP_LOCKLEAF | CNP_NOOBJ,
1653 UIO_USERSPACE, uap->path, td);
1655 error = kern_stat(&nd, &st);
1658 error = copyout(&st, uap->ub, sizeof(*uap->ub));
1663 * lstat_args(char *path, struct stat *ub)
1665 * Get file status; this version does not follow links.
1668 lstat(struct lstat_args *uap)
1670 struct thread *td = curthread;
1671 struct nameidata nd;
1675 NDINIT(&nd, NAMEI_LOOKUP, CNP_LOCKLEAF | CNP_NOOBJ,
1676 UIO_USERSPACE, SCARG(uap, path), td);
1678 error = kern_stat(&nd, &st);
1681 error = copyout(&st, uap->ub, sizeof(*uap->ub));
1690 nsb->st_dev = sb->st_dev;
1691 nsb->st_ino = sb->st_ino;
1692 nsb->st_mode = sb->st_mode;
1693 nsb->st_nlink = sb->st_nlink;
1694 nsb->st_uid = sb->st_uid;
1695 nsb->st_gid = sb->st_gid;
1696 nsb->st_rdev = sb->st_rdev;
1697 nsb->st_atimespec = sb->st_atimespec;
1698 nsb->st_mtimespec = sb->st_mtimespec;
1699 nsb->st_ctimespec = sb->st_ctimespec;
1700 nsb->st_size = sb->st_size;
1701 nsb->st_blocks = sb->st_blocks;
1702 nsb->st_blksize = sb->st_blksize;
1703 nsb->st_flags = sb->st_flags;
1704 nsb->st_gen = sb->st_gen;
1705 nsb->st_qspare[0] = sb->st_qspare[0];
1706 nsb->st_qspare[1] = sb->st_qspare[1];
1710 * nstat_args(char *path, struct nstat *ub)
1714 nstat(struct nstat_args *uap)
1716 struct thread *td = curthread;
1720 struct nameidata nd;
1722 NDINIT(&nd, NAMEI_LOOKUP, CNP_FOLLOW | CNP_LOCKLEAF | CNP_NOOBJ,
1723 UIO_USERSPACE, SCARG(uap, path), td);
1724 if ((error = namei(&nd)) != 0)
1726 NDFREE(&nd, NDF_ONLY_PNBUF);
1727 error = vn_stat(nd.ni_vp, &sb, td);
1731 cvtnstat(&sb, &nsb);
1732 error = copyout(&nsb, SCARG(uap, ub), sizeof (nsb));
1737 * lstat_args(char *path, struct stat *ub)
1739 * Get file status; this version does not follow links.
1743 nlstat(struct nlstat_args *uap)
1745 struct thread *td = curthread;
1750 struct nameidata nd;
1752 NDINIT(&nd, NAMEI_LOOKUP, CNP_LOCKLEAF | CNP_NOOBJ,
1753 UIO_USERSPACE, SCARG(uap, path), td);
1754 if ((error = namei(&nd)) != 0)
1757 NDFREE(&nd, NDF_ONLY_PNBUF);
1758 error = vn_stat(vp, &sb, td);
1762 cvtnstat(&sb, &nsb);
1763 error = copyout(&nsb, SCARG(uap, ub), sizeof (nsb));
1768 * pathconf_Args(char *path, int name)
1770 * Get configurable pathname variables.
1774 pathconf(struct pathconf_args *uap)
1776 struct thread *td = curthread;
1778 struct nameidata nd;
1780 NDINIT(&nd, NAMEI_LOOKUP, CNP_FOLLOW | CNP_LOCKLEAF | CNP_NOOBJ,
1781 UIO_USERSPACE, SCARG(uap, path), td);
1782 if ((error = namei(&nd)) != 0)
1784 NDFREE(&nd, NDF_ONLY_PNBUF);
1785 error = VOP_PATHCONF(nd.ni_vp, SCARG(uap, name), uap->sysmsg_fds);
1792 * kern_readlink isn't properly split yet. There is a copyin burried
1793 * in VOP_READLINK().
1796 kern_readlink(struct nameidata *nd, char *buf, int count, int *res)
1798 struct thread *td = curthread;
1799 struct proc *p = td->td_proc;
1808 NDFREE(nd, NDF_ONLY_PNBUF);
1810 if (vp->v_type != VLNK)
1813 aiov.iov_base = buf;
1814 aiov.iov_len = count;
1815 auio.uio_iov = &aiov;
1816 auio.uio_iovcnt = 1;
1817 auio.uio_offset = 0;
1818 auio.uio_rw = UIO_READ;
1819 auio.uio_segflg = UIO_USERSPACE;
1821 auio.uio_resid = count;
1822 error = VOP_READLINK(vp, &auio, p->p_ucred);
1825 *res = count - auio.uio_resid;
1830 * readlink_args(char *path, char *buf, int count)
1832 * Return target name of a symbolic link.
1835 readlink(struct readlink_args *uap)
1837 struct thread *td = curthread;
1838 struct nameidata nd;
1841 NDINIT(&nd, NAMEI_LOOKUP, CNP_LOCKLEAF | CNP_NOOBJ, UIO_USERSPACE,
1844 error = kern_readlink(&nd, uap->buf, uap->count,
1845 &uap->sysmsg_result);
1851 setfflags(struct vnode *vp, int flags)
1853 struct thread *td = curthread;
1854 struct proc *p = td->td_proc;
1859 * Prevent non-root users from setting flags on devices. When
1860 * a device is reused, users can retain ownership of the device
1861 * if they are allowed to set flags and programs assume that
1862 * chown can't fail when done as root.
1864 if ((vp->v_type == VCHR || vp->v_type == VBLK) &&
1865 ((error = suser_cred(p->p_ucred, PRISON_ROOT)) != 0))
1868 VOP_LEASE(vp, td, p->p_ucred, LEASE_WRITE);
1869 vn_lock(vp, NULL, LK_EXCLUSIVE | LK_RETRY, td);
1871 vattr.va_flags = flags;
1872 error = VOP_SETATTR(vp, &vattr, p->p_ucred, td);
1873 VOP_UNLOCK(vp, NULL, 0, td);
1878 * chflags(char *path, int flags)
1880 * Change flags of a file given a path name.
1884 chflags(struct chflags_args *uap)
1886 struct thread *td = curthread;
1888 struct nameidata nd;
1890 NDINIT(&nd, NAMEI_LOOKUP, CNP_FOLLOW, UIO_USERSPACE,
1891 SCARG(uap, path), td);
1892 if ((error = namei(&nd)) != 0)
1894 NDFREE(&nd, NDF_ONLY_PNBUF);
1895 error = setfflags(nd.ni_vp, SCARG(uap, flags));
1901 * fchflags_args(int fd, int flags)
1903 * Change flags of a file given a file descriptor.
1907 fchflags(struct fchflags_args *uap)
1909 struct thread *td = curthread;
1910 struct proc *p = td->td_proc;
1914 if ((error = getvnode(p->p_fd, SCARG(uap, fd), &fp)) != 0)
1916 return setfflags((struct vnode *) fp->f_data, SCARG(uap, flags));
1920 setfmode(struct vnode *vp, int mode)
1922 struct thread *td = curthread;
1923 struct proc *p = td->td_proc;
1927 VOP_LEASE(vp, td, p->p_ucred, LEASE_WRITE);
1928 vn_lock(vp, NULL, LK_EXCLUSIVE | LK_RETRY, td);
1930 vattr.va_mode = mode & ALLPERMS;
1931 error = VOP_SETATTR(vp, &vattr, p->p_ucred, td);
1932 VOP_UNLOCK(vp, NULL, 0, td);
1937 kern_chmod(struct nameidata *nd, int mode)
1944 NDFREE(nd, NDF_ONLY_PNBUF);
1945 error = setfmode(nd->ni_vp, mode);
1951 * chmod_args(char *path, int mode)
1953 * Change mode of a file given path name.
1957 chmod(struct chmod_args *uap)
1959 struct thread *td = curthread;
1960 struct nameidata nd;
1963 NDINIT(&nd, NAMEI_LOOKUP, CNP_FOLLOW, UIO_USERSPACE, uap->path, td);
1965 error = kern_chmod(&nd, uap->mode);
1971 * lchmod_args(char *path, int mode)
1973 * Change mode of a file given path name (don't follow links.)
1977 lchmod(struct lchmod_args *uap)
1979 struct thread *td = curthread;
1981 struct nameidata nd;
1983 NDINIT(&nd, NAMEI_LOOKUP, 0, UIO_USERSPACE, SCARG(uap, path), td);
1984 if ((error = namei(&nd)) != 0)
1986 NDFREE(&nd, NDF_ONLY_PNBUF);
1987 error = setfmode(nd.ni_vp, SCARG(uap, mode));
1993 * fchmod_args(int fd, int mode)
1995 * Change mode of a file given a file descriptor.
1999 fchmod(struct fchmod_args *uap)
2001 struct thread *td = curthread;
2002 struct proc *p = td->td_proc;
2006 if ((error = getvnode(p->p_fd, SCARG(uap, fd), &fp)) != 0)
2008 return setfmode((struct vnode *)fp->f_data, SCARG(uap, mode));
2012 setfown(struct vnode *vp, uid_t uid, gid_t gid)
2014 struct thread *td = curthread;
2015 struct proc *p = td->td_proc;
2019 VOP_LEASE(vp, td, p->p_ucred, LEASE_WRITE);
2020 vn_lock(vp, NULL, LK_EXCLUSIVE | LK_RETRY, td);
2024 error = VOP_SETATTR(vp, &vattr, p->p_ucred, td);
2025 VOP_UNLOCK(vp, NULL, 0, td);
2030 kern_chown(struct nameidata *nd, int uid, int gid)
2037 NDFREE(nd, NDF_ONLY_PNBUF);
2038 error = setfown(nd->ni_vp, uid, gid);
2044 * chown(char *path, int uid, int gid)
2046 * Set ownership given a path name.
2049 chown(struct chown_args *uap)
2051 struct thread *td = curthread;
2052 struct nameidata nd;
2055 NDINIT(&nd, NAMEI_LOOKUP, CNP_FOLLOW, UIO_USERSPACE, uap->path, td);
2057 error = kern_chown(&nd, uap->uid, uap->gid);
2063 * lchown_args(char *path, int uid, int gid)
2065 * Set ownership given a path name, do not cross symlinks.
2068 lchown(struct lchown_args *uap)
2070 struct thread *td = curthread;
2072 struct nameidata nd;
2074 NDINIT(&nd, NAMEI_LOOKUP, 0, UIO_USERSPACE, uap->path, td);
2076 error = kern_chown(&nd, uap->uid, uap->gid);
2082 * fchown_args(int fd, int uid, int gid)
2084 * Set ownership given a file descriptor.
2088 fchown(struct fchown_args *uap)
2090 struct thread *td = curthread;
2091 struct proc *p = td->td_proc;
2095 if ((error = getvnode(p->p_fd, SCARG(uap, fd), &fp)) != 0)
2097 return setfown((struct vnode *)fp->f_data,
2098 SCARG(uap, uid), SCARG(uap, gid));
2102 getutimes(const struct timeval *tvp, struct timespec *tsp)
2104 struct timeval tv[2];
2108 TIMEVAL_TO_TIMESPEC(&tv[0], &tsp[0]);
2111 TIMEVAL_TO_TIMESPEC(&tvp[0], &tsp[0]);
2112 TIMEVAL_TO_TIMESPEC(&tvp[1], &tsp[1]);
2118 setutimes(struct vnode *vp, const struct timespec *ts, int nullflag)
2120 struct thread *td = curthread;
2121 struct proc *p = td->td_proc;
2125 VOP_LEASE(vp, td, p->p_ucred, LEASE_WRITE);
2126 vn_lock(vp, NULL, LK_EXCLUSIVE | LK_RETRY, td);
2128 vattr.va_atime = ts[0];
2129 vattr.va_mtime = ts[1];
2131 vattr.va_vaflags |= VA_UTIMES_NULL;
2132 error = VOP_SETATTR(vp, &vattr, p->p_ucred, td);
2133 VOP_UNLOCK(vp, NULL, 0, td);
2138 kern_utimes(struct nameidata *nd, struct timeval *tptr)
2140 struct timespec ts[2];
2143 error = getutimes(tptr, ts);
2149 NDFREE(nd, NDF_ONLY_PNBUF);
2150 error = setutimes(nd->ni_vp, ts, tptr == NULL);
2156 * utimes_args(char *path, struct timeval *tptr)
2158 * Set the access and modification times of a file.
2161 utimes(struct utimes_args *uap)
2163 struct thread *td = curthread;
2164 struct timeval tv[2];
2165 struct nameidata nd;
2169 error = copyin(uap->tptr, tv, sizeof(tv));
2173 NDINIT(&nd, NAMEI_LOOKUP, CNP_FOLLOW, UIO_USERSPACE, uap->path, td);
2175 error = kern_utimes(&nd, uap->tptr ? tv : NULL);
2181 * lutimes_args(char *path, struct timeval *tptr)
2183 * Set the access and modification times of a file.
2186 lutimes(struct lutimes_args *uap)
2188 struct thread *td = curthread;
2189 struct timeval tv[2];
2190 struct nameidata nd;
2194 error = copyin(uap->tptr, tv, sizeof(tv));
2198 NDINIT(&nd, NAMEI_LOOKUP, 0, UIO_USERSPACE, uap->path, td);
2200 error = kern_utimes(&nd, uap->tptr ? tv : NULL);
2206 kern_futimes(int fd, struct timeval *tptr)
2208 struct thread *td = curthread;
2209 struct proc *p = td->td_proc;
2210 struct timespec ts[2];
2214 error = getutimes(tptr, ts);
2217 error = getvnode(p->p_fd, fd, &fp);
2220 error = setutimes((struct vnode *)fp->f_data, ts, tptr == NULL);
2225 * futimes_args(int fd, struct timeval *tptr)
2227 * Set the access and modification times of a file.
2230 futimes(struct futimes_args *uap)
2232 struct timeval tv[2];
2236 error = copyin(uap->tptr, tv, sizeof(tv));
2241 error = kern_futimes(uap->fd, uap->tptr ? tv : NULL);
2247 kern_truncate(struct nameidata* nd, off_t length)
2249 struct thread *td = curthread;
2250 struct proc *p = td->td_proc;
2257 if ((error = namei(nd)) != 0)
2260 NDFREE(nd, NDF_ONLY_PNBUF);
2261 VOP_LEASE(vp, td, p->p_ucred, LEASE_WRITE);
2262 vn_lock(vp, NULL, LK_EXCLUSIVE | LK_RETRY, td);
2263 if (vp->v_type == VDIR)
2265 else if ((error = vn_writechk(vp)) == 0 &&
2266 (error = VOP_ACCESS(vp, VWRITE, p->p_ucred, td)) == 0) {
2268 vattr.va_size = length;
2269 error = VOP_SETATTR(vp, &vattr, p->p_ucred, td);
2276 * truncate(char *path, int pad, off_t length)
2278 * Truncate a file given its path name.
2281 truncate(struct truncate_args *uap)
2283 struct thread *td = curthread;
2284 struct nameidata nd;
2287 NDINIT(&nd, NAMEI_LOOKUP, CNP_FOLLOW, UIO_USERSPACE, uap->path, td);
2289 error = kern_truncate(&nd, uap->length);
2295 kern_ftruncate(int fd, off_t length)
2297 struct thread *td = curthread;
2298 struct proc *p = td->td_proc;
2306 if ((error = getvnode(p->p_fd, fd, &fp)) != 0)
2308 if ((fp->f_flag & FWRITE) == 0)
2310 vp = (struct vnode *)fp->f_data;
2311 VOP_LEASE(vp, td, p->p_ucred, LEASE_WRITE);
2312 vn_lock(vp, NULL, LK_EXCLUSIVE | LK_RETRY, td);
2313 if (vp->v_type == VDIR)
2315 else if ((error = vn_writechk(vp)) == 0) {
2317 vattr.va_size = length;
2318 error = VOP_SETATTR(vp, &vattr, fp->f_cred, td);
2320 VOP_UNLOCK(vp, NULL, 0, td);
2325 * ftruncate_args(int fd, int pad, off_t length)
2327 * Truncate a file given a file descriptor.
2330 ftruncate(struct ftruncate_args *uap)
2334 error = kern_ftruncate(uap->fd, uap->length);
2342 * Sync an open file.
2346 fsync(struct fsync_args *uap)
2348 struct thread *td = curthread;
2349 struct proc *p = td->td_proc;
2355 if ((error = getvnode(p->p_fd, SCARG(uap, fd), &fp)) != 0)
2357 vp = (struct vnode *)fp->f_data;
2358 vn_lock(vp, NULL, LK_EXCLUSIVE | LK_RETRY, td);
2359 if (VOP_GETVOBJECT(vp, &obj) == 0)
2360 vm_object_page_clean(obj, 0, 0, 0);
2361 if ((error = VOP_FSYNC(vp, MNT_WAIT, td)) == 0 &&
2362 vp->v_mount && (vp->v_mount->mnt_flag & MNT_SOFTDEP) &&
2364 error = (*bioops.io_fsync)(vp);
2365 VOP_UNLOCK(vp, NULL, 0, td);
2370 kern_rename(struct nameidata *fromnd, struct nameidata *tond)
2372 struct thread *td = curthread;
2373 struct proc *p = td->td_proc;
2374 struct vnode *tvp, *fvp, *tdvp;
2378 error = namei(fromnd);
2381 fvp = fromnd->ni_vp;
2382 if (fromnd->ni_vp->v_type == VDIR)
2383 tond->ni_cnd.cn_flags |= CNP_WILLBEDIR;
2384 error = namei(tond);
2386 /* Translate error code for rename("dir1", "dir2/."). */
2387 if (error == EISDIR && fvp->v_type == VDIR)
2389 NDFREE(fromnd, NDF_ONLY_PNBUF);
2390 vrele(fromnd->ni_dvp);
2394 tdvp = tond->ni_dvp;
2397 if (fvp->v_type == VDIR && tvp->v_type != VDIR) {
2400 } else if (fvp->v_type != VDIR && tvp->v_type == VDIR) {
2408 * If the source is the same as the destination (that is, if they
2409 * are links to the same vnode), then there is nothing to do.
2415 VOP_LEASE(tdvp, td, p->p_ucred, LEASE_WRITE);
2416 if (fromnd->ni_dvp != tdvp) {
2417 VOP_LEASE(fromnd->ni_dvp, td, p->p_ucred, LEASE_WRITE);
2420 VOP_LEASE(tvp, td, p->p_ucred, LEASE_WRITE);
2422 error = VOP_RENAME(fromnd->ni_dvp, NCPNULL, fromnd->ni_vp,
2423 &fromnd->ni_cnd, tond->ni_dvp, NCPNULL, tond->ni_vp,
2425 NDFREE(fromnd, NDF_ONLY_PNBUF);
2426 NDFREE(tond, NDF_ONLY_PNBUF);
2428 NDFREE(fromnd, NDF_ONLY_PNBUF);
2429 NDFREE(tond, NDF_ONLY_PNBUF);
2436 vrele(fromnd->ni_dvp);
2439 vrele(tond->ni_startdir);
2440 ASSERT_VOP_UNLOCKED(fromnd->ni_dvp, "rename");
2441 ASSERT_VOP_UNLOCKED(fromnd->ni_vp, "rename");
2442 ASSERT_VOP_UNLOCKED(tond->ni_dvp, "rename");
2443 ASSERT_VOP_UNLOCKED(tond->ni_vp, "rename");
2445 if (fromnd->ni_startdir)
2446 vrele(fromnd->ni_startdir);
2453 * rename_args(char *from, char *to)
2455 * Rename files. Source and destination must either both be directories,
2456 * or both not be directories. If target is a directory, it must be empty.
2459 rename(struct rename_args *uap)
2461 struct thread *td = curthread;
2462 struct nameidata fromnd, tond;
2465 NDINIT(&fromnd, NAMEI_DELETE, CNP_WANTPARENT | CNP_SAVESTART,
2466 UIO_USERSPACE, uap->from, td);
2467 NDINIT(&tond, NAMEI_RENAME,
2468 CNP_LOCKPARENT | CNP_LOCKLEAF | CNP_NOCACHE |
2469 CNP_SAVESTART | CNP_NOOBJ,
2470 UIO_USERSPACE, uap->to, td);
2472 error = kern_rename(&fromnd, &tond);
2478 kern_mkdir(struct nameidata *nd, int mode)
2480 struct thread *td = curthread;
2481 struct proc *p = td->td_proc;
2487 nd->ni_cnd.cn_flags |= CNP_WILLBEDIR;
2493 NDFREE(nd, NDF_ONLY_PNBUF);
2494 if (nd->ni_dvp == vp)
2502 vattr.va_type = VDIR;
2503 vattr.va_mode = (mode & ACCESSPERMS) &~ p->p_fd->fd_cmask;
2504 VOP_LEASE(nd->ni_dvp, td, p->p_ucred, LEASE_WRITE);
2505 error = VOP_MKDIR(nd->ni_dvp, NCPNULL, &nd->ni_vp, &nd->ni_cnd,
2507 NDFREE(nd, NDF_ONLY_PNBUF);
2511 ASSERT_VOP_UNLOCKED(nd->ni_dvp, "mkdir");
2512 ASSERT_VOP_UNLOCKED(nd->ni_vp, "mkdir");
2517 * mkdir_args(char *path, int mode)
2519 * Make a directory file.
2523 mkdir(struct mkdir_args *uap)
2525 struct thread *td = curthread;
2526 struct nameidata nd;
2529 NDINIT(&nd, NAMEI_CREATE, CNP_LOCKPARENT, UIO_USERSPACE, uap->path,
2532 error = kern_mkdir(&nd, uap->mode);
2538 kern_rmdir(struct nameidata *nd)
2540 struct thread *td = curthread;
2541 struct proc *p = td->td_proc;
2550 if (vp->v_type != VDIR) {
2555 * No rmdir "." please.
2557 if (nd->ni_dvp == vp) {
2562 * The root of a mounted filesystem cannot be deleted.
2564 if (vp->v_flag & VROOT)
2567 VOP_LEASE(nd->ni_dvp, td, p->p_ucred, LEASE_WRITE);
2568 VOP_LEASE(vp, td, p->p_ucred, LEASE_WRITE);
2569 error = VOP_RMDIR(nd->ni_dvp, NCPNULL, nd->ni_vp,
2573 NDFREE(nd, NDF_ONLY_PNBUF);
2574 if (nd->ni_dvp == vp)
2580 ASSERT_VOP_UNLOCKED(nd->ni_dvp, "rmdir");
2581 ASSERT_VOP_UNLOCKED(nd->ni_vp, "rmdir");
2586 * rmdir_args(char *path)
2588 * Remove a directory file.
2592 rmdir(struct rmdir_args *uap)
2594 struct thread *td = curthread;
2595 struct nameidata nd;
2598 NDINIT(&nd, NAMEI_DELETE, CNP_LOCKPARENT | CNP_LOCKLEAF,
2599 UIO_USERSPACE, uap->path, td);
2601 error = kern_rmdir(&nd);
2607 kern_getdirentries(int fd, char *buf, u_int count, long *basep, int *res)
2609 struct thread *td = curthread;
2610 struct proc *p = td->td_proc;
2618 if ((error = getvnode(p->p_fd, fd, &fp)) != 0)
2620 if ((fp->f_flag & FREAD) == 0)
2622 vp = (struct vnode *)fp->f_data;
2624 if (vp->v_type != VDIR)
2626 aiov.iov_base = buf;
2627 aiov.iov_len = count;
2628 auio.uio_iov = &aiov;
2629 auio.uio_iovcnt = 1;
2630 auio.uio_rw = UIO_READ;
2631 auio.uio_segflg = UIO_USERSPACE;
2633 auio.uio_resid = count;
2634 /* vn_lock(vp, NULL, LK_SHARED | LK_RETRY, td); */
2635 vn_lock(vp, NULL, LK_EXCLUSIVE | LK_RETRY, td);
2636 loff = auio.uio_offset = fp->f_offset;
2637 error = VOP_READDIR(vp, &auio, fp->f_cred, &eofflag, NULL, NULL);
2638 fp->f_offset = auio.uio_offset;
2639 VOP_UNLOCK(vp, NULL, 0, td);
2642 if (count == auio.uio_resid) {
2643 if (union_dircheckp) {
2644 error = union_dircheckp(td, &vp, fp);
2650 if ((vp->v_flag & VROOT) &&
2651 (vp->v_mount->mnt_flag & MNT_UNION)) {
2652 struct vnode *tvp = vp;
2653 vp = vp->v_mount->mnt_vnodecovered;
2655 fp->f_data = (caddr_t)vp;
2664 *res = count - auio.uio_resid;
2669 * getdirentries_args(int fd, char *buf, u_int conut, long *basep)
2671 * Read a block of directory entries in a file system independent format.
2674 getdirentries(struct getdirentries_args *uap)
2679 error = kern_getdirentries(uap->fd, uap->buf, uap->count, &base,
2680 &uap->sysmsg_result);
2683 error = copyout(&base, uap->basep, sizeof(*uap->basep));
2688 * getdents_args(int fd, char *buf, size_t count)
2691 getdents(struct getdents_args *uap)
2695 error = kern_getdirentries(uap->fd, uap->buf, uap->count, NULL,
2696 &uap->sysmsg_result);
2702 * umask(int newmask)
2704 * Set the mode mask for creation of filesystem nodes.
2709 umask(struct umask_args *uap)
2711 struct thread *td = curthread;
2712 struct proc *p = td->td_proc;
2713 struct filedesc *fdp;
2716 uap->sysmsg_result = fdp->fd_cmask;
2717 fdp->fd_cmask = SCARG(uap, newmask) & ALLPERMS;
2722 * revoke(char *path)
2724 * Void all references to file by ripping underlying filesystem
2729 revoke(struct revoke_args *uap)
2731 struct thread *td = curthread;
2732 struct proc *p = td->td_proc;
2736 struct nameidata nd;
2738 NDINIT(&nd, NAMEI_LOOKUP, CNP_FOLLOW, UIO_USERSPACE, SCARG(uap, path), td);
2739 if ((error = namei(&nd)) != 0)
2742 NDFREE(&nd, NDF_ONLY_PNBUF);
2743 if (vp->v_type != VCHR && vp->v_type != VBLK) {
2747 if ((error = VOP_GETATTR(vp, &vattr, td)) != 0)
2749 if (p->p_ucred->cr_uid != vattr.va_uid &&
2750 (error = suser_cred(p->p_ucred, PRISON_ROOT)))
2752 if (count_udev(vp->v_udev) > 0)
2753 VOP_REVOKE(vp, REVOKEALL);
2760 * Convert a user file descriptor to a kernel file entry.
2763 getvnode(struct filedesc *fdp, int fd, struct file **fpp)
2767 if ((u_int)fd >= fdp->fd_nfiles ||
2768 (fp = fdp->fd_ofiles[fd]) == NULL)
2770 if (fp->f_type != DTYPE_VNODE && fp->f_type != DTYPE_FIFO)
2776 * getfh_args(char *fname, fhandle_t *fhp)
2778 * Get (NFS) file handle
2781 getfh(struct getfh_args *uap)
2783 struct thread *td = curthread;
2784 struct nameidata nd;
2790 * Must be super user
2795 NDINIT(&nd, NAMEI_LOOKUP, CNP_FOLLOW | CNP_LOCKLEAF, UIO_USERSPACE, uap->fname, td);
2799 NDFREE(&nd, NDF_ONLY_PNBUF);
2801 bzero(&fh, sizeof(fh));
2802 fh.fh_fsid = vp->v_mount->mnt_stat.f_fsid;
2803 error = VFS_VPTOFH(vp, &fh.fh_fid);
2807 error = copyout(&fh, uap->fhp, sizeof (fh));
2812 * fhopen_args(const struct fhandle *u_fhp, int flags)
2814 * syscall for the rpc.lockd to use to translate a NFS file handle into
2815 * an open descriptor.
2817 * warning: do not remove the suser() call or this becomes one giant
2821 fhopen(struct fhopen_args *uap)
2823 struct thread *td = curthread;
2824 struct proc *p = td->td_proc;
2829 struct vattr *vap = &vat;
2832 struct filedesc *fdp = p->p_fd;
2833 int fmode, mode, error, type;
2838 * Must be super user
2844 fmode = FFLAGS(SCARG(uap, flags));
2845 /* why not allow a non-read/write open for our lockd? */
2846 if (((fmode & (FREAD | FWRITE)) == 0) || (fmode & O_CREAT))
2848 error = copyin(SCARG(uap,u_fhp), &fhp, sizeof(fhp));
2851 /* find the mount point */
2852 mp = vfs_getvfs(&fhp.fh_fsid);
2855 /* now give me my vnode, it gets returned to me locked */
2856 error = VFS_FHTOVP(mp, &fhp.fh_fid, &vp);
2860 * from now on we have to make sure not
2861 * to forget about the vnode
2862 * any error that causes an abort must vput(vp)
2863 * just set error = err and 'goto bad;'.
2869 if (vp->v_type == VLNK) {
2873 if (vp->v_type == VSOCK) {
2878 if (fmode & (FWRITE | O_TRUNC)) {
2879 if (vp->v_type == VDIR) {
2883 error = vn_writechk(vp);
2891 error = VOP_ACCESS(vp, mode, p->p_ucred, td);
2895 if (fmode & O_TRUNC) {
2896 VOP_UNLOCK(vp, NULL, 0, td); /* XXX */
2897 VOP_LEASE(vp, td, p->p_ucred, LEASE_WRITE);
2898 vn_lock(vp, NULL, LK_EXCLUSIVE | LK_RETRY, td); /* XXX */
2901 error = VOP_SETATTR(vp, vap, p->p_ucred, td);
2905 error = VOP_OPEN(vp, fmode, p->p_ucred, td);
2909 * Make sure that a VM object is created for VMIO support.
2911 if (vn_canvmio(vp) == TRUE) {
2912 if ((error = vfs_object_create(vp, td)) != 0)
2919 * end of vn_open code
2922 if ((error = falloc(p, &nfp, &indx)) != 0) {
2930 * hold an extra reference to avoid having fp ripped out
2931 * from under us while we block in the lock op.
2934 nfp->f_data = (caddr_t)vp;
2935 nfp->f_flag = fmode & FMASK;
2936 nfp->f_ops = &vnops;
2937 nfp->f_type = DTYPE_VNODE;
2938 if (fmode & (O_EXLOCK | O_SHLOCK)) {
2939 lf.l_whence = SEEK_SET;
2942 if (fmode & O_EXLOCK)
2943 lf.l_type = F_WRLCK;
2945 lf.l_type = F_RDLCK;
2947 if ((fmode & FNONBLOCK) == 0)
2949 VOP_UNLOCK(vp, NULL, 0, td);
2950 if ((error = VOP_ADVLOCK(vp, (caddr_t)fp, F_SETLK, &lf, type)) != 0) {
2952 * lock request failed. Normally close the descriptor
2953 * but handle the case where someone might have dup()d
2954 * or close()d it when we weren't looking.
2956 if (fdp->fd_ofiles[indx] == fp) {
2957 fdp->fd_ofiles[indx] = NULL;
2962 * release our private reference.
2967 vn_lock(vp, NULL, LK_EXCLUSIVE | LK_RETRY, td);
2968 fp->f_flag |= FHASLOCK;
2970 if ((vp->v_type == VREG) && (VOP_GETVOBJECT(vp, NULL) != 0))
2971 vfs_object_create(vp, td);
2973 VOP_UNLOCK(vp, NULL, 0, td);
2975 uap->sysmsg_result = indx;
2984 * fhstat_args(struct fhandle *u_fhp, struct stat *sb)
2987 fhstat(struct fhstat_args *uap)
2989 struct thread *td = curthread;
2997 * Must be super user
3003 error = copyin(SCARG(uap, u_fhp), &fh, sizeof(fhandle_t));
3007 if ((mp = vfs_getvfs(&fh.fh_fsid)) == NULL)
3009 if ((error = VFS_FHTOVP(mp, &fh.fh_fid, &vp)))
3011 error = vn_stat(vp, &sb, td);
3015 error = copyout(&sb, SCARG(uap, sb), sizeof(sb));
3020 * fhstatfs_args(struct fhandle *u_fhp, struct statfs *buf)
3023 fhstatfs(struct fhstatfs_args *uap)
3025 struct thread *td = curthread;
3034 * Must be super user
3036 if ((error = suser(td)))
3039 if ((error = copyin(SCARG(uap, u_fhp), &fh, sizeof(fhandle_t))) != 0)
3042 if ((mp = vfs_getvfs(&fh.fh_fsid)) == NULL)
3044 if ((error = VFS_FHTOVP(mp, &fh.fh_fid, &vp)))
3049 if ((error = VFS_STATFS(mp, sp, td)) != 0)
3051 sp->f_flags = mp->mnt_flag & MNT_VISFLAGMASK;
3053 bcopy(sp, &sb, sizeof(sb));
3054 sb.f_fsid.val[0] = sb.f_fsid.val[1] = 0;
3057 return (copyout(sp, SCARG(uap, buf), sizeof(*sp)));
3061 * Syscall to push extended attribute configuration information into the
3062 * VFS. Accepts a path, which it converts to a mountpoint, as well as
3063 * a command (int cmd), and attribute name and misc data. For now, the
3064 * attribute name is left in userspace for consumption by the VFS_op.
3065 * It will probably be changed to be copied into sysspace by the
3066 * syscall in the future, once issues with various consumers of the
3067 * attribute code have raised their hands.
3069 * Currently this is used only by UFS Extended Attributes.
3072 extattrctl(struct extattrctl_args *uap)
3074 struct thread *td = curthread;
3075 struct nameidata nd;
3079 NDINIT(&nd, NAMEI_LOOKUP, CNP_FOLLOW, UIO_USERSPACE, SCARG(uap, path), td);
3080 if ((error = namei(&nd)) != 0)
3082 mp = nd.ni_vp->v_mount;
3084 return (VFS_EXTATTRCTL(mp, SCARG(uap, cmd), SCARG(uap, attrname),
3085 SCARG(uap, arg), td));
3089 * Syscall to set a named extended attribute on a file or directory.
3090 * Accepts attribute name, and a uio structure pointing to the data to set.
3091 * The uio is consumed in the style of writev(). The real work happens
3092 * in VOP_SETEXTATTR().
3095 extattr_set_file(struct extattr_set_file_args *uap)
3097 struct thread *td = curthread;
3098 struct proc *p = td->td_proc;
3099 struct nameidata nd;
3101 struct iovec *iov, *needfree = NULL, aiov[UIO_SMALLIOV];
3102 char attrname[EXTATTR_MAXNAMELEN];
3106 error = copyin(SCARG(uap, attrname), attrname, EXTATTR_MAXNAMELEN);
3109 NDINIT(&nd, NAMEI_LOOKUP, CNP_FOLLOW | CNP_LOCKLEAF, UIO_USERSPACE,
3110 SCARG(uap, path), td);
3111 if ((error = namei(&nd)) != 0)
3113 iovlen = uap->iovcnt * sizeof(struct iovec);
3114 if (uap->iovcnt > UIO_SMALLIOV) {
3115 if (uap->iovcnt > UIO_MAXIOV) {
3119 MALLOC(iov, struct iovec *, iovlen, M_IOV, M_WAITOK);
3124 auio.uio_iovcnt = uap->iovcnt;
3125 auio.uio_rw = UIO_WRITE;
3126 auio.uio_segflg = UIO_USERSPACE;
3128 auio.uio_offset = 0;
3129 if ((error = copyin(uap->iovp, iov, iovlen)))
3132 for (i = 0; i < uap->iovcnt; i++) {
3133 if (iov->iov_len > INT_MAX - auio.uio_resid) {
3137 auio.uio_resid += iov->iov_len;
3140 cnt = auio.uio_resid;
3141 error = VOP_SETEXTATTR(nd.ni_vp, attrname, &auio, p->p_ucred, td);
3142 cnt -= auio.uio_resid;
3143 uap->sysmsg_result = cnt;
3146 FREE(needfree, M_IOV);
3152 * Syscall to get a named extended attribute on a file or directory.
3153 * Accepts attribute name, and a uio structure pointing to a buffer for the
3154 * data. The uio is consumed in the style of readv(). The real work
3155 * happens in VOP_GETEXTATTR();
3158 extattr_get_file(struct extattr_get_file_args *uap)
3160 struct thread *td = curthread;
3161 struct proc *p = td->td_proc;
3162 struct nameidata nd;
3164 struct iovec *iov, *needfree, aiov[UIO_SMALLIOV];
3165 char attrname[EXTATTR_MAXNAMELEN];
3169 error = copyin(SCARG(uap, attrname), attrname, EXTATTR_MAXNAMELEN);
3172 NDINIT(&nd, NAMEI_LOOKUP, CNP_FOLLOW | CNP_LOCKLEAF, UIO_USERSPACE,
3173 SCARG(uap, path), td);
3174 if ((error = namei(&nd)) != 0)
3176 iovlen = uap->iovcnt * sizeof (struct iovec);
3177 if (uap->iovcnt > UIO_SMALLIOV) {
3178 if (uap->iovcnt > UIO_MAXIOV) {
3182 MALLOC(iov, struct iovec *, iovlen, M_IOV, M_WAITOK);
3189 auio.uio_iovcnt = uap->iovcnt;
3190 auio.uio_rw = UIO_READ;
3191 auio.uio_segflg = UIO_USERSPACE;
3193 auio.uio_offset = 0;
3194 if ((error = copyin(uap->iovp, iov, iovlen)))
3197 for (i = 0; i < uap->iovcnt; i++) {
3198 if (iov->iov_len > INT_MAX - auio.uio_resid) {
3202 auio.uio_resid += iov->iov_len;
3205 cnt = auio.uio_resid;
3206 error = VOP_GETEXTATTR(nd.ni_vp, attrname, &auio, p->p_ucred, td);
3207 cnt -= auio.uio_resid;
3208 uap->sysmsg_result = cnt;
3211 FREE(needfree, M_IOV);
3217 * Syscall to delete a named extended attribute from a file or directory.
3218 * Accepts attribute name. The real work happens in VOP_SETEXTATTR().
3221 extattr_delete_file(struct extattr_delete_file_args *uap)
3223 struct thread *td = curthread;
3224 struct proc *p = td->td_proc;
3225 struct nameidata nd;
3226 char attrname[EXTATTR_MAXNAMELEN];
3229 error = copyin(SCARG(uap, attrname), attrname, EXTATTR_MAXNAMELEN);
3232 NDINIT(&nd, NAMEI_LOOKUP, CNP_FOLLOW | CNP_LOCKLEAF, UIO_USERSPACE,
3233 SCARG(uap, path), td);
3234 if ((error = namei(&nd)) != 0)
3236 error = VOP_SETEXTATTR(nd.ni_vp, attrname, NULL, p->p_ucred, td);
3242 * print out statistics from the current status of the buffer pool
3243 * this can be toggeled by the system control option debug.syncprt
3252 int counts[(MAXBSIZE / PAGE_SIZE) + 1];
3253 static char *bname[3] = { "LOCKED", "LRU", "AGE" };
3255 for (dp = bufqueues, i = 0; dp < &bufqueues[3]; dp++, i++) {
3257 for (j = 0; j <= MAXBSIZE/PAGE_SIZE; j++)
3260 TAILQ_FOREACH(bp, dp, b_freelist) {
3261 counts[bp->b_bufsize/PAGE_SIZE]++;
3265 printf("%s: total-%d", bname[i], count);
3266 for (j = 0; j <= MAXBSIZE/PAGE_SIZE; j++)
3268 printf(", %d-%d", j * PAGE_SIZE, counts[j]);