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.55 2005/01/31 17:20:48 joerg 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/nlookup.h>
62 #include <sys/dirent.h>
63 #include <sys/extattr.h>
64 #include <sys/kern_syscall.h>
66 #include <machine/limits.h>
67 #include <vfs/union/union.h>
68 #include <sys/sysctl.h>
70 #include <vm/vm_object.h>
71 #include <vm/vm_zone.h>
72 #include <vm/vm_page.h>
74 #include <sys/file2.h>
76 static int checkvp_chdir (struct vnode *vn, struct thread *td);
77 static void checkdirs (struct vnode *olddp, struct namecache *ncp);
78 static int chroot_refuse_vdir_fds (struct filedesc *fdp);
79 static int getutimes (const struct timeval *, struct timespec *);
80 static int setfown (struct vnode *, uid_t, gid_t);
81 static int setfmode (struct vnode *, int);
82 static int setfflags (struct vnode *, int);
83 static int setutimes (struct vnode *, const struct timespec *, int);
84 static int usermount = 0; /* if 1, non-root can mount fs. */
86 int (*union_dircheckp) (struct thread *, struct vnode **, struct file *);
88 SYSCTL_INT(_vfs, OID_AUTO, usermount, CTLFLAG_RW, &usermount, 0, "");
91 * Virtual File System System Calls
95 * Mount a file system.
98 * mount_args(char *type, char *path, int flags, caddr_t data)
102 mount(struct mount_args *uap)
104 struct thread *td = curthread;
105 struct proc *p = td->td_proc;
107 struct namecache *ncp;
109 struct vfsconf *vfsp;
110 int error, flag = 0, flag2 = 0;
112 struct nlookupdata nd;
113 char fstypename[MFSNAMELEN];
115 struct nlcomponent nlc;
118 if (p->p_ucred->cr_prison != NULL)
120 if (usermount == 0 && (error = suser(td)))
123 * Do not allow NFS export by non-root users.
125 if (SCARG(uap, flags) & MNT_EXPORTED) {
131 * Silently enforce MNT_NOSUID and MNT_NODEV for non-root users
134 SCARG(uap, flags) |= MNT_NOSUID | MNT_NODEV;
137 * Lookup the requested path and extract the ncp and vnode.
139 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
141 if ((error = nlookup(&nd)) == 0) {
142 if (nd.nl_ncp->nc_vp == NULL)
152 * Extract the locked+refd ncp and cleanup the nd structure
159 * now we have the locked ref'd ncp and unreferenced vnode.
162 if ((error = vget(vp, LK_EXCLUSIVE, td)) != 0) {
169 * Now we have an unlocked ref'd ncp and a locked ref'd vp
171 if (SCARG(uap, flags) & MNT_UPDATE) {
172 if ((vp->v_flag & VROOT) == 0) {
179 flag2 = mp->mnt_kern_flag;
181 * We only allow the filesystem to be reloaded if it
182 * is currently mounted read-only.
184 if ((SCARG(uap, flags) & MNT_RELOAD) &&
185 ((mp->mnt_flag & MNT_RDONLY) == 0)) {
188 return (EOPNOTSUPP); /* Needs translation */
191 * Only root, or the user that did the original mount is
192 * permitted to update it.
194 if (mp->mnt_stat.f_owner != p->p_ucred->cr_uid &&
195 (error = suser(td))) {
200 if (vfs_busy(mp, LK_NOWAIT, NULL, td)) {
205 if ((vp->v_flag & VMOUNT) != 0 ||
206 vp->v_mountedhere != NULL) {
212 vp->v_flag |= VMOUNT;
214 SCARG(uap, flags) & (MNT_RELOAD | MNT_FORCE | MNT_UPDATE);
215 VOP_UNLOCK(vp, 0, td);
219 * If the user is not root, ensure that they own the directory
220 * onto which we are attempting to mount.
222 if ((error = VOP_GETATTR(vp, &va, td)) ||
223 (va.va_uid != p->p_ucred->cr_uid &&
224 (error = suser(td)))) {
229 if ((error = vinvalbuf(vp, V_SAVE, td, 0, 0)) != 0) {
234 if (vp->v_type != VDIR) {
239 if ((error = copyinstr(SCARG(uap, type), fstypename, MFSNAMELEN, NULL)) != 0) {
244 for (vfsp = vfsconf; vfsp; vfsp = vfsp->vfc_next) {
245 if (!strcmp(vfsp->vfc_name, fstypename))
251 /* Only load modules for root (very important!) */
252 if ((error = suser(td)) != 0) {
257 error = linker_load_file(fstypename, &lf);
258 if (error || lf == NULL) {
266 /* lookup again, see if the VFS was loaded */
267 for (vfsp = vfsconf; vfsp; vfsp = vfsp->vfc_next) {
268 if (!strcmp(vfsp->vfc_name, fstypename))
273 linker_file_unload(lf);
279 if ((vp->v_flag & VMOUNT) != 0 ||
280 vp->v_mountedhere != NULL) {
285 vp->v_flag |= VMOUNT;
288 * Allocate and initialize the filesystem.
290 mp = malloc(sizeof(struct mount), M_MOUNT, M_ZERO|M_WAITOK);
291 TAILQ_INIT(&mp->mnt_nvnodelist);
292 TAILQ_INIT(&mp->mnt_reservedvnlist);
293 TAILQ_INIT(&mp->mnt_jlist);
294 mp->mnt_nvnodelistsize = 0;
295 lockinit(&mp->mnt_lock, 0, "vfslock", 0, LK_NOPAUSE);
296 vfs_busy(mp, LK_NOWAIT, NULL, td);
297 mp->mnt_op = vfsp->vfc_vfsops;
299 vfsp->vfc_refcount++;
300 mp->mnt_stat.f_type = vfsp->vfc_typenum;
301 mp->mnt_flag |= vfsp->vfc_flags & MNT_VISFLAGMASK;
302 strncpy(mp->mnt_stat.f_fstypename, vfsp->vfc_name, MFSNAMELEN);
303 mp->mnt_vnodecovered = vp;
304 mp->mnt_stat.f_owner = p->p_ucred->cr_uid;
305 mp->mnt_iosize_max = DFLTPHYS;
306 VOP_UNLOCK(vp, 0, td);
309 * Set the mount level flags.
311 if (SCARG(uap, flags) & MNT_RDONLY)
312 mp->mnt_flag |= MNT_RDONLY;
313 else if (mp->mnt_flag & MNT_RDONLY)
314 mp->mnt_kern_flag |= MNTK_WANTRDWR;
315 mp->mnt_flag &=~ (MNT_NOSUID | MNT_NOEXEC | MNT_NODEV |
316 MNT_SYNCHRONOUS | MNT_UNION | MNT_ASYNC | MNT_NOATIME |
317 MNT_NOSYMFOLLOW | MNT_IGNORE |
318 MNT_NOCLUSTERR | MNT_NOCLUSTERW | MNT_SUIDDIR);
319 mp->mnt_flag |= SCARG(uap, flags) & (MNT_NOSUID | MNT_NOEXEC |
320 MNT_NODEV | MNT_SYNCHRONOUS | MNT_UNION | MNT_ASYNC | MNT_FORCE |
321 MNT_NOSYMFOLLOW | MNT_IGNORE |
322 MNT_NOATIME | MNT_NOCLUSTERR | MNT_NOCLUSTERW | MNT_SUIDDIR);
324 * Mount the filesystem.
325 * XXX The final recipients of VFS_MOUNT just overwrite the ndp they
328 error = VFS_MOUNT(mp, SCARG(uap, path), SCARG(uap, data), td);
329 if (mp->mnt_flag & MNT_UPDATE) {
330 if (mp->mnt_kern_flag & MNTK_WANTRDWR)
331 mp->mnt_flag &= ~MNT_RDONLY;
332 mp->mnt_flag &=~ (MNT_UPDATE | MNT_RELOAD | MNT_FORCE);
333 mp->mnt_kern_flag &=~ MNTK_WANTRDWR;
336 mp->mnt_kern_flag = flag2;
339 vp->v_flag &= ~VMOUNT;
344 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY, td);
346 * Put the new filesystem on the mount list after root. The mount
347 * point gets its own mnt_ncp which is a special ncp linking the
348 * vnode-under to the root of the new mount. The lookup code
349 * detects the mount point going forward and detects the special
350 * mnt_ncp via NCP_MOUNTPT going backwards.
352 * It is not necessary to invalidate or purge the vnode underneath
353 * because elements under the mount will be given their own glue
357 nlc.nlc_nameptr = "";
359 mp->mnt_ncp = cache_nlookup(ncp, &nlc);
360 cache_setunresolved(mp->mnt_ncp);
361 mp->mnt_ncp->nc_flag |= NCF_MOUNTPT;
362 mp->mnt_ncp->nc_mount = mp;
364 /* XXX get the root of the fs and cache_setvp(mnt_ncp...) */
365 vp->v_flag &= ~VMOUNT;
366 vp->v_mountedhere = mp;
367 lwkt_gettoken(&ilock, &mountlist_token);
368 TAILQ_INSERT_TAIL(&mountlist, mp, mnt_list);
369 lwkt_reltoken(&ilock);
370 checkdirs(vp, mp->mnt_ncp);
371 cache_unlock(mp->mnt_ncp); /* leave ref intact */
372 VOP_UNLOCK(vp, 0, td);
373 error = vfs_allocate_syncvnode(mp);
375 if ((error = VFS_START(mp, 0, td)) != 0)
378 vfs_rm_vnodeops(&mp->mnt_vn_coherency_ops);
379 vfs_rm_vnodeops(&mp->mnt_vn_journal_ops);
380 vfs_rm_vnodeops(&mp->mnt_vn_norm_ops);
381 vfs_rm_vnodeops(&mp->mnt_vn_spec_ops);
382 vfs_rm_vnodeops(&mp->mnt_vn_fifo_ops);
383 vp->v_flag &= ~VMOUNT;
384 mp->mnt_vfc->vfc_refcount--;
394 * Scan all active processes to see if any of them have a current
395 * or root directory onto which the new filesystem has just been
396 * mounted. If so, replace them with the new mount point.
398 * The passed ncp is ref'd and locked (from the mount code) and
399 * must be associated with the vnode representing the root of the
403 checkdirs(struct vnode *olddp, struct namecache *ncp)
405 struct filedesc *fdp;
410 if (olddp->v_usecount == 1)
412 mp = olddp->v_mountedhere;
413 if (VFS_ROOT(mp, &newdp))
414 panic("mount: lost mount");
415 cache_setvp(ncp, newdp);
417 if (rootvnode == olddp) {
419 vfs_cache_setroot(newdp, cache_hold(ncp));
422 FOREACH_PROC_IN_SYSTEM(p) {
424 if (fdp->fd_cdir == olddp) {
427 fdp->fd_cdir = newdp;
428 cache_drop(fdp->fd_ncdir);
429 fdp->fd_ncdir = cache_hold(ncp);
431 if (fdp->fd_rdir == olddp) {
434 fdp->fd_rdir = newdp;
435 cache_drop(fdp->fd_nrdir);
436 fdp->fd_nrdir = cache_hold(ncp);
443 * Unmount a file system.
445 * Note: unmount takes a path to the vnode mounted on as argument,
446 * not special file (as before).
449 * umount_args(char *path, int flags)
453 unmount(struct unmount_args *uap)
455 struct thread *td = curthread;
456 struct proc *p = td->td_proc;
460 struct nlookupdata nd;
463 if (p->p_ucred->cr_prison != NULL)
465 if (usermount == 0 && (error = suser(td)))
469 error = nlookup_init(&nd, SCARG(uap, path), UIO_USERSPACE, NLC_FOLLOW);
471 error = nlookup(&nd);
473 error = cache_vget(nd.nl_ncp, nd.nl_cred, LK_EXCLUSIVE, &vp);
481 * Only root, or the user that did the original mount is
482 * permitted to unmount this filesystem.
484 if ((mp->mnt_stat.f_owner != p->p_ucred->cr_uid) &&
485 (error = suser(td))) {
491 * Don't allow unmounting the root file system.
493 if (mp->mnt_flag & MNT_ROOTFS) {
499 * Must be the root of the filesystem
501 if ((vp->v_flag & VROOT) == 0) {
506 return (dounmount(mp, SCARG(uap, flags), td));
510 * Do the actual file system unmount.
513 dounmount(struct mount *mp, int flags, struct thread *td)
515 struct vnode *coveredvp;
520 lwkt_gettoken(&ilock, &mountlist_token);
521 if (mp->mnt_kern_flag & MNTK_UNMOUNT) {
522 lwkt_reltoken(&ilock);
525 mp->mnt_kern_flag |= MNTK_UNMOUNT;
526 /* Allow filesystems to detect that a forced unmount is in progress. */
527 if (flags & MNT_FORCE)
528 mp->mnt_kern_flag |= MNTK_UNMOUNTF;
529 error = lockmgr(&mp->mnt_lock, LK_DRAIN | LK_INTERLOCK |
530 ((flags & MNT_FORCE) ? 0 : LK_NOWAIT), &ilock, td);
532 mp->mnt_kern_flag &= ~(MNTK_UNMOUNT | MNTK_UNMOUNTF);
533 if (mp->mnt_kern_flag & MNTK_MWAIT)
538 if (mp->mnt_flag & MNT_EXPUBLIC)
539 vfs_setpublicfs(NULL, NULL, NULL);
541 vfs_msync(mp, MNT_WAIT);
542 async_flag = mp->mnt_flag & MNT_ASYNC;
543 mp->mnt_flag &=~ MNT_ASYNC;
544 cache_purgevfs(mp); /* remove cache entries for this file sys */
545 if (mp->mnt_syncer != NULL)
546 vrele(mp->mnt_syncer);
547 if (((mp->mnt_flag & MNT_RDONLY) ||
548 (error = VFS_SYNC(mp, MNT_WAIT, td)) == 0) ||
550 error = VFS_UNMOUNT(mp, flags, td);
551 lwkt_gettokref(&ilock);
553 if (mp->mnt_syncer == NULL)
554 vfs_allocate_syncvnode(mp);
555 mp->mnt_kern_flag &= ~(MNTK_UNMOUNT | MNTK_UNMOUNTF);
556 mp->mnt_flag |= async_flag;
557 lockmgr(&mp->mnt_lock, LK_RELEASE | LK_INTERLOCK | LK_REENABLE,
559 if (mp->mnt_kern_flag & MNTK_MWAIT)
563 TAILQ_REMOVE(&mountlist, mp, mnt_list);
566 * Remove any installed vnode ops here so the individual VFSs don't
569 vfs_rm_vnodeops(&mp->mnt_vn_coherency_ops);
570 vfs_rm_vnodeops(&mp->mnt_vn_journal_ops);
571 vfs_rm_vnodeops(&mp->mnt_vn_norm_ops);
572 vfs_rm_vnodeops(&mp->mnt_vn_spec_ops);
573 vfs_rm_vnodeops(&mp->mnt_vn_fifo_ops);
575 if ((coveredvp = mp->mnt_vnodecovered) != NULLVP) {
576 coveredvp->v_mountedhere = NULL;
578 cache_drop(mp->mnt_ncp);
581 mp->mnt_vfc->vfc_refcount--;
582 if (!TAILQ_EMPTY(&mp->mnt_nvnodelist))
583 panic("unmount: dangling vnode");
584 lockmgr(&mp->mnt_lock, LK_RELEASE | LK_INTERLOCK, &ilock, td);
585 if (mp->mnt_kern_flag & MNTK_MWAIT)
592 * Sync each mounted filesystem.
596 static int syncprt = 0;
597 SYSCTL_INT(_debug, OID_AUTO, syncprt, CTLFLAG_RW, &syncprt, 0, "");
602 sync(struct sync_args *uap)
604 struct thread *td = curthread;
605 struct mount *mp, *nmp;
609 lwkt_gettoken(&ilock, &mountlist_token);
610 for (mp = TAILQ_FIRST(&mountlist); mp != NULL; mp = nmp) {
611 if (vfs_busy(mp, LK_NOWAIT, &ilock, td)) {
612 nmp = TAILQ_NEXT(mp, mnt_list);
615 if ((mp->mnt_flag & MNT_RDONLY) == 0) {
616 asyncflag = mp->mnt_flag & MNT_ASYNC;
617 mp->mnt_flag &= ~MNT_ASYNC;
618 vfs_msync(mp, MNT_NOWAIT);
619 VFS_SYNC(mp, MNT_NOWAIT, td);
620 mp->mnt_flag |= asyncflag;
622 lwkt_gettokref(&ilock);
623 nmp = TAILQ_NEXT(mp, mnt_list);
626 lwkt_reltoken(&ilock);
628 * print out buffer pool stat information on each sync() call.
637 /* XXX PRISON: could be per prison flag */
638 static int prison_quotas;
640 SYSCTL_INT(_kern_prison, OID_AUTO, quotas, CTLFLAG_RW, &prison_quotas, 0, "");
644 * quotactl_args(char *path, int fcmd, int uid, caddr_t arg)
646 * Change filesystem quotas.
650 quotactl(struct quotactl_args *uap)
652 struct nlookupdata nd;
660 if (p->p_ucred->cr_prison && !prison_quotas)
663 error = nlookup_init(&nd, SCARG(uap, path), UIO_USERSPACE, NLC_FOLLOW);
665 error = nlookup(&nd);
667 mp = nd.nl_ncp->nc_mount;
668 error = VFS_QUOTACTL(mp, SCARG(uap, cmd), SCARG(uap, uid),
669 SCARG(uap, arg), nd.nl_td);
676 * mountctl(char *path, int op, int fd, const void *ctl, int ctllen,
677 * void *buf, int buflen)
679 * This function operates on a mount point and executes the specified
680 * operation using the specified control data, and possibly returns data.
682 * The actual number of bytes stored in the result buffer is returned, 0
683 * if none, otherwise an error is returned.
687 mountctl(struct mountctl_args *uap)
689 struct thread *td = curthread;
690 struct proc *p = td->td_proc;
691 struct filedesc *fdp = p->p_fd;
699 * Sanity and permissions checks. We must be root.
702 if (p->p_ucred->cr_prison != NULL)
704 if ((error = suser(td)) != 0)
708 * Argument length checks
710 if (uap->ctllen < 0 || uap->ctllen > 1024)
712 if (uap->buflen < 0 || uap->buflen > 16 * 1024)
714 if (uap->path == NULL)
718 * Allocate the necessary buffers and copyin data
720 path = zalloc(namei_zone);
721 error = copyinstr(uap->path, path, MAXPATHLEN, NULL);
726 ctl = malloc(uap->ctllen + 1, M_TEMP, M_WAITOK|M_ZERO);
727 error = copyin(uap->ctl, ctl, uap->ctllen);
732 buf = malloc(uap->buflen + 1, M_TEMP, M_WAITOK|M_ZERO);
735 * Validate the descriptor
739 } else if ((u_int)uap->fd >= fdp->fd_nfiles ||
740 (fp = fdp->fd_ofiles[uap->fd]) == NULL) {
748 * Execute the internal kernel function and clean up.
750 error = kern_mountctl(path, uap->op, fp, ctl, uap->ctllen, buf, uap->buflen, &uap->sysmsg_result);
753 if (error == 0 && uap->sysmsg_result > 0)
754 error = copyout(buf, uap->buf, uap->sysmsg_result);
757 zfree(namei_zone, path);
766 * Execute a mount control operation by resolving the path to a mount point
767 * and calling vop_mountctl().
770 kern_mountctl(const char *path, int op, struct file *fp,
771 const void *ctl, int ctllen,
772 void *buf, int buflen, int *res)
776 struct nlookupdata nd;
781 error = nlookup_init(&nd, path, UIO_SYSSPACE, NLC_FOLLOW);
783 error = nlookup(&nd);
785 error = cache_vget(nd.nl_ncp, nd.nl_cred, LK_EXCLUSIVE, &vp);
793 * Must be the root of the filesystem
795 if ((vp->v_flag & VROOT) == 0) {
799 error = vop_mountctl(mp->mnt_vn_use_ops, op, fp, ctl, ctllen,
806 kern_statfs(struct nlookupdata *nd, struct statfs *buf)
808 struct thread *td = curthread;
813 if ((error = nlookup(nd)) != 0)
815 mp = nd->nl_ncp->nc_mount;
817 if ((error = VFS_STATFS(mp, sp, td)) != 0)
819 sp->f_flags = mp->mnt_flag & MNT_VISFLAGMASK;
820 bcopy(sp, buf, sizeof(*buf));
821 /* Only root should have access to the fsid's. */
823 buf->f_fsid.val[0] = buf->f_fsid.val[1] = 0;
828 * statfs_args(char *path, struct statfs *buf)
830 * Get filesystem statistics.
833 statfs(struct statfs_args *uap)
835 struct nlookupdata nd;
839 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
841 error = kern_statfs(&nd, &buf);
844 error = copyout(&buf, uap->buf, sizeof(*uap->buf));
849 kern_fstatfs(int fd, struct statfs *buf)
851 struct thread *td = curthread;
852 struct proc *p = td->td_proc;
859 error = getvnode(p->p_fd, fd, &fp);
862 mp = ((struct vnode *)fp->f_data)->v_mount;
866 error = VFS_STATFS(mp, sp, td);
869 sp->f_flags = mp->mnt_flag & MNT_VISFLAGMASK;
870 bcopy(sp, buf, sizeof(*buf));
871 /* Only root should have access to the fsid's. */
873 buf->f_fsid.val[0] = buf->f_fsid.val[1] = 0;
878 * fstatfs_args(int fd, struct statfs *buf)
880 * Get filesystem statistics.
883 fstatfs(struct fstatfs_args *uap)
888 error = kern_fstatfs(uap->fd, &buf);
891 error = copyout(&buf, uap->buf, sizeof(*uap->buf));
896 * getfsstat_args(struct statfs *buf, long bufsize, int flags)
898 * Get statistics on all filesystems.
902 getfsstat(struct getfsstat_args *uap)
904 struct thread *td = curthread;
905 struct mount *mp, *nmp;
909 long count, maxcount, error;
911 maxcount = SCARG(uap, bufsize) / sizeof(struct statfs);
912 sfsp = (caddr_t)SCARG(uap, buf);
914 lwkt_gettoken(&ilock, &mountlist_token);
915 for (mp = TAILQ_FIRST(&mountlist); mp != NULL; mp = nmp) {
916 if (vfs_busy(mp, LK_NOWAIT, &ilock, td)) {
917 nmp = TAILQ_NEXT(mp, mnt_list);
920 if (sfsp && count < maxcount) {
923 * If MNT_NOWAIT or MNT_LAZY is specified, do not
924 * refresh the fsstat cache. MNT_NOWAIT or MNT_LAZY
925 * overrides MNT_WAIT.
927 if (((SCARG(uap, flags) & (MNT_LAZY|MNT_NOWAIT)) == 0 ||
928 (SCARG(uap, flags) & MNT_WAIT)) &&
929 (error = VFS_STATFS(mp, sp, td))) {
930 lwkt_gettokref(&ilock);
931 nmp = TAILQ_NEXT(mp, mnt_list);
935 sp->f_flags = mp->mnt_flag & MNT_VISFLAGMASK;
936 error = copyout(sp, sfsp, sizeof(*sp));
944 lwkt_gettokref(&ilock);
945 nmp = TAILQ_NEXT(mp, mnt_list);
948 lwkt_reltoken(&ilock);
949 if (sfsp && count > maxcount)
950 uap->sysmsg_result = maxcount;
952 uap->sysmsg_result = count;
957 * fchdir_args(int fd)
959 * Change current working directory to a given file descriptor.
963 fchdir(struct fchdir_args *uap)
965 struct thread *td = curthread;
966 struct proc *p = td->td_proc;
967 struct filedesc *fdp = p->p_fd;
968 struct vnode *vp, *ovp;
971 struct namecache *ncp, *oncp;
972 struct namecache *nct;
975 if ((error = getvnode(fdp, SCARG(uap, fd), &fp)) != 0)
977 vp = (struct vnode *)fp->f_data;
979 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY, td);
980 if (vp->v_type != VDIR || fp->f_ncp == NULL)
983 error = VOP_ACCESS(vp, VEXEC, p->p_ucred, td);
988 ncp = cache_hold(fp->f_ncp);
989 while (!error && (mp = vp->v_mountedhere) != NULL) {
990 error = nlookup_mp(mp, &nct);
992 cache_unlock(nct); /* leave ref intact */
995 error = vget(vp, LK_SHARED, td);
996 KKASSERT(error == 0);
1003 oncp = fdp->fd_ncdir;
1004 VOP_UNLOCK(vp, 0, td); /* leave ref intact */
1006 fdp->fd_ncdir = ncp;
1017 kern_chdir(struct nlookupdata *nd)
1019 struct thread *td = curthread;
1020 struct proc *p = td->td_proc;
1021 struct filedesc *fdp = p->p_fd;
1022 struct vnode *vp, *ovp;
1023 struct namecache *oncp;
1026 if ((error = nlookup(nd)) != 0)
1028 if ((vp = nd->nl_ncp->nc_vp) == NULL)
1030 if ((error = vget(vp, LK_SHARED, td)) != 0)
1033 error = checkvp_chdir(vp, td);
1034 VOP_UNLOCK(vp, 0, td);
1037 oncp = fdp->fd_ncdir;
1038 cache_unlock(nd->nl_ncp); /* leave reference intact */
1039 fdp->fd_ncdir = nd->nl_ncp;
1051 * chdir_args(char *path)
1053 * Change current working directory (``.'').
1056 chdir(struct chdir_args *uap)
1058 struct nlookupdata nd;
1061 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
1063 error = kern_chdir(&nd);
1069 * Helper function for raised chroot(2) security function: Refuse if
1070 * any filedescriptors are open directories.
1073 chroot_refuse_vdir_fds(fdp)
1074 struct filedesc *fdp;
1081 for (fd = 0; fd < fdp->fd_nfiles ; fd++) {
1082 error = getvnode(fdp, fd, &fp);
1085 vp = (struct vnode *)fp->f_data;
1086 if (vp->v_type != VDIR)
1094 * This sysctl determines if we will allow a process to chroot(2) if it
1095 * has a directory open:
1096 * 0: disallowed for all processes.
1097 * 1: allowed for processes that were not already chroot(2)'ed.
1098 * 2: allowed for all processes.
1101 static int chroot_allow_open_directories = 1;
1103 SYSCTL_INT(_kern, OID_AUTO, chroot_allow_open_directories, CTLFLAG_RW,
1104 &chroot_allow_open_directories, 0, "");
1107 * chroot to the specified namecache entry. We obtain the vp from the
1108 * namecache data. The passed ncp must be locked and referenced and will
1109 * remain locked and referenced on return.
1112 kern_chroot(struct namecache *ncp)
1114 struct thread *td = curthread;
1115 struct proc *p = td->td_proc;
1116 struct filedesc *fdp = p->p_fd;
1121 * Only root can chroot
1123 if ((error = suser_cred(p->p_ucred, PRISON_ROOT)) != 0)
1127 * Disallow open directory descriptors (fchdir() breakouts).
1129 if (chroot_allow_open_directories == 0 ||
1130 (chroot_allow_open_directories == 1 && fdp->fd_rdir != rootvnode)) {
1131 if ((error = chroot_refuse_vdir_fds(fdp)) != 0)
1134 if ((vp = ncp->nc_vp) == NULL)
1137 if ((error = vget(vp, LK_SHARED, td)) != 0)
1141 * Check the validity of vp as a directory to change to and
1142 * associate it with rdir/jdir.
1144 error = checkvp_chdir(vp, td);
1145 VOP_UNLOCK(vp, 0, td); /* leave reference intact */
1147 vrele(fdp->fd_rdir);
1148 fdp->fd_rdir = vp; /* reference inherited by fd_rdir */
1149 cache_drop(fdp->fd_nrdir);
1150 fdp->fd_nrdir = cache_hold(ncp);
1151 if (fdp->fd_jdir == NULL) {
1154 fdp->fd_njdir = cache_hold(ncp);
1163 * chroot_args(char *path)
1165 * Change notion of root (``/'') directory.
1169 chroot(struct chroot_args *uap)
1171 struct thread *td = curthread;
1172 struct nlookupdata nd;
1175 KKASSERT(td->td_proc);
1176 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
1181 error = nlookup(&nd);
1183 error = kern_chroot(nd.nl_ncp);
1189 * Common routine for chroot and chdir. Given a locked, referenced vnode,
1190 * determine whether it is legal to chdir to the vnode. The vnode's state
1191 * is not changed by this call.
1194 checkvp_chdir(struct vnode *vp, struct thread *td)
1198 if (vp->v_type != VDIR)
1201 error = VOP_ACCESS(vp, VEXEC, td->td_proc->p_ucred, td);
1206 kern_open(struct nlookupdata *nd, int oflags, int mode, int *res)
1208 struct thread *td = curthread;
1209 struct proc *p = td->td_proc;
1210 struct filedesc *fdp = p->p_fd;
1215 int type, indx, error;
1218 if ((oflags & O_ACCMODE) == O_ACCMODE)
1220 flags = FFLAGS(oflags);
1221 error = falloc(p, &nfp, NULL);
1225 cmode = ((mode &~ fdp->fd_cmask) & ALLPERMS) &~ S_ISTXT;
1228 * XXX p_dupfd is a real mess. It allows a device to return a
1229 * file descriptor to be duplicated rather then doing the open
1235 * Call vn_open() to do the lookup and assign the vnode to the
1236 * file pointer. vn_open() does not change the ref count on fp
1237 * and the vnode, on success, will be inherited by the file pointer
1240 nd->nl_flags |= NLC_LOCKVP;
1241 error = vn_open(nd, fp, flags, cmode);
1245 * handle special fdopen() case. bleh. dupfdopen() is
1246 * responsible for dropping the old contents of ofiles[indx]
1249 * Note that if fsetfd() succeeds it will add a ref to fp
1250 * which represents the fd_ofiles[] assignment. We must still
1251 * drop our reference.
1253 if ((error == ENODEV || error == ENXIO) && p->p_dupfd >= 0) {
1254 if (fsetfd(p, fp, &indx) == 0) {
1255 error = dupfdopen(fdp, indx, p->p_dupfd, flags, error);
1258 fdrop(fp, td); /* our ref */
1261 if (fdp->fd_ofiles[indx] == fp) {
1262 fdp->fd_ofiles[indx] = NULL;
1263 fdrop(fp, td); /* fd_ofiles[] ref */
1267 fdrop(fp, td); /* our ref */
1268 if (error == ERESTART)
1274 * ref the vnode for ourselves so it can't be ripped out from under
1275 * is. XXX need an ND flag to request that the vnode be returned
1278 vp = (struct vnode *)fp->f_data;
1280 if ((error = fsetfd(p, fp, &indx)) != 0) {
1287 * If no error occurs the vp will have been assigned to the file
1293 * There should be 2 references on the file, one from the descriptor
1294 * table, and one for us.
1296 * Handle the case where someone closed the file (via its file
1297 * descriptor) while we were blocked. The end result should look
1298 * like opening the file succeeded but it was immediately closed.
1300 if (fp->f_count == 1) {
1301 KASSERT(fdp->fd_ofiles[indx] != fp,
1302 ("Open file descriptor lost all refs"));
1310 if (flags & (O_EXLOCK | O_SHLOCK)) {
1311 lf.l_whence = SEEK_SET;
1314 if (flags & O_EXLOCK)
1315 lf.l_type = F_WRLCK;
1317 lf.l_type = F_RDLCK;
1319 if ((flags & FNONBLOCK) == 0)
1322 if ((error = VOP_ADVLOCK(vp, (caddr_t)fp, F_SETLK, &lf, type)) != 0) {
1324 * lock request failed. Normally close the descriptor
1325 * but handle the case where someone might have dup()d
1326 * it when we weren't looking. One reference is
1327 * owned by the descriptor array, the other by us.
1330 if (fdp->fd_ofiles[indx] == fp) {
1331 fdp->fd_ofiles[indx] = NULL;
1337 fp->f_flag |= FHASLOCK;
1339 /* assert that vn_open created a backing object if one is needed */
1340 KASSERT(!vn_canvmio(vp) || VOP_GETVOBJECT(vp, NULL) == 0,
1341 ("open: vmio vnode has no backing object after vn_open"));
1346 * release our private reference, leaving the one associated with the
1347 * descriptor table intact.
1355 * open_args(char *path, int flags, int mode)
1357 * Check permissions, allocate an open file structure,
1358 * and call the device open routine if any.
1361 open(struct open_args *uap)
1363 struct nlookupdata nd;
1366 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
1368 error = kern_open(&nd, uap->flags,
1369 uap->mode, &uap->sysmsg_result);
1376 kern_mknod(struct nlookupdata *nd, int mode, int dev)
1378 struct namecache *ncp;
1379 struct thread *td = curthread;
1380 struct proc *p = td->td_proc;
1388 switch (mode & S_IFMT) {
1394 error = suser_cred(p->p_ucred, PRISON_ROOT);
1401 nd->nl_flags |= NLC_CREATE;
1402 if ((error = nlookup(nd)) != 0)
1409 vattr.va_mode = (mode & ALLPERMS) &~ p->p_fd->fd_cmask;
1410 vattr.va_rdev = dev;
1413 switch (mode & S_IFMT) {
1414 case S_IFMT: /* used by badsect to flag bad sectors */
1415 vattr.va_type = VBAD;
1418 vattr.va_type = VCHR;
1421 vattr.va_type = VBLK;
1432 error = VOP_NWHITEOUT(ncp, nd->nl_cred, NAMEI_CREATE);
1435 error = VOP_NMKNOD(ncp, &vp, nd->nl_cred, &vattr);
1444 * mknod_args(char *path, int mode, int dev)
1446 * Create a special file.
1449 mknod(struct mknod_args *uap)
1451 struct nlookupdata nd;
1454 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, 0);
1456 error = kern_mknod(&nd, uap->mode, uap->dev);
1462 kern_mkfifo(struct nlookupdata *nd, int mode)
1464 struct namecache *ncp;
1465 struct thread *td = curthread;
1466 struct proc *p = td->td_proc;
1473 nd->nl_flags |= NLC_CREATE;
1474 if ((error = nlookup(nd)) != 0)
1481 vattr.va_type = VFIFO;
1482 vattr.va_mode = (mode & ALLPERMS) &~ p->p_fd->fd_cmask;
1484 error = VOP_NMKNOD(ncp, &vp, nd->nl_cred, &vattr);
1491 * mkfifo_args(char *path, int mode)
1493 * Create a named pipe.
1496 mkfifo(struct mkfifo_args *uap)
1498 struct nlookupdata nd;
1501 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, 0);
1503 error = kern_mkfifo(&nd, uap->mode);
1509 kern_link(struct nlookupdata *nd, struct nlookupdata *linknd)
1511 struct thread *td = curthread;
1516 * Lookup the source and obtained a locked vnode.
1518 * XXX relookup on vget failure / race ?
1521 if ((error = nlookup(nd)) != 0)
1523 vp = nd->nl_ncp->nc_vp;
1524 KKASSERT(vp != NULL);
1525 if (vp->v_type == VDIR)
1526 return (EPERM); /* POSIX */
1527 if ((error = vget(vp, LK_EXCLUSIVE, td)) != 0)
1531 * Unlock the source so we can lookup the target without deadlocking
1532 * (XXX vp is locked already, possible other deadlock?). The target
1535 KKASSERT(nd->nl_flags & NLC_NCPISLOCKED);
1536 nd->nl_flags &= ~NLC_NCPISLOCKED;
1537 cache_unlock(nd->nl_ncp);
1539 linknd->nl_flags |= NLC_CREATE;
1540 if ((error = nlookup(linknd)) != 0) {
1544 if (linknd->nl_ncp->nc_vp) {
1550 * Finally run the new API VOP.
1552 error = VOP_NLINK(linknd->nl_ncp, vp, linknd->nl_cred);
1558 * link_args(char *path, char *link)
1560 * Make a hard file link.
1563 link(struct link_args *uap)
1565 struct nlookupdata nd, linknd;
1568 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
1570 error = nlookup_init(&linknd, uap->link, UIO_USERSPACE, 0);
1572 error = kern_link(&nd, &linknd);
1573 nlookup_done(&linknd);
1580 kern_symlink(struct nlookupdata *nd, char *path, int mode)
1582 struct namecache *ncp;
1588 nd->nl_flags |= NLC_CREATE;
1589 if ((error = nlookup(nd)) != 0)
1596 vattr.va_mode = mode;
1597 error = VOP_NSYMLINK(ncp, &vp, nd->nl_cred, &vattr, path);
1604 * symlink(char *path, char *link)
1606 * Make a symbolic link.
1609 symlink(struct symlink_args *uap)
1611 struct thread *td = curthread;
1612 struct nlookupdata nd;
1617 path = zalloc(namei_zone);
1618 error = copyinstr(uap->path, path, MAXPATHLEN, NULL);
1620 error = nlookup_init(&nd, uap->link, UIO_USERSPACE, 0);
1622 mode = ACCESSPERMS & ~td->td_proc->p_fd->fd_cmask;
1623 error = kern_symlink(&nd, path, mode);
1627 zfree(namei_zone, path);
1632 * undelete_args(char *path)
1634 * Delete a whiteout from the filesystem.
1638 undelete(struct undelete_args *uap)
1640 struct nlookupdata nd;
1643 error = nlookup_init(&nd, SCARG(uap, path), UIO_USERSPACE, 0);
1645 nd.nl_flags |= NLC_DELETE;
1647 error = nlookup(&nd);
1649 error = VOP_NWHITEOUT(nd.nl_ncp, nd.nl_cred, NAMEI_DELETE);
1655 kern_unlink(struct nlookupdata *nd)
1657 struct namecache *ncp;
1661 nd->nl_flags |= NLC_DELETE;
1662 if ((error = nlookup(nd)) != 0)
1665 error = VOP_NREMOVE(ncp, nd->nl_cred);
1670 * unlink_args(char *path)
1672 * Delete a name from the filesystem.
1675 unlink(struct unlink_args *uap)
1677 struct nlookupdata nd;
1680 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, 0);
1682 error = kern_unlink(&nd);
1688 kern_lseek(int fd, off_t offset, int whence, off_t *res)
1690 struct thread *td = curthread;
1691 struct proc *p = td->td_proc;
1692 struct filedesc *fdp = p->p_fd;
1697 if ((u_int)fd >= fdp->fd_nfiles ||
1698 (fp = fdp->fd_ofiles[fd]) == NULL)
1700 if (fp->f_type != DTYPE_VNODE)
1704 fp->f_offset += offset;
1707 error=VOP_GETATTR((struct vnode *)fp->f_data, &vattr, td);
1710 fp->f_offset = offset + vattr.va_size;
1713 fp->f_offset = offset;
1718 *res = fp->f_offset;
1723 * lseek_args(int fd, int pad, off_t offset, int whence)
1725 * Reposition read/write file offset.
1728 lseek(struct lseek_args *uap)
1732 error = kern_lseek(uap->fd, uap->offset, uap->whence,
1733 &uap->sysmsg_offset);
1739 kern_access(struct nlookupdata *nd, int aflags)
1741 struct thread *td = curthread;
1745 if ((error = nlookup(nd)) != 0)
1748 error = cache_vget(nd->nl_ncp, nd->nl_cred, LK_EXCLUSIVE, &vp);
1752 /* Flags == 0 means only check for existence. */
1761 if ((flags & VWRITE) == 0 || (error = vn_writechk(vp)) == 0)
1762 error = VOP_ACCESS(vp, flags, nd->nl_cred, td);
1765 * If the file handle is stale we have to re-resolve the
1766 * entry. This is a hack at the moment.
1768 if (error == ESTALE) {
1769 cache_setunresolved(nd->nl_ncp);
1770 error = cache_resolve(nd->nl_ncp, nd->nl_cred);
1783 * access_args(char *path, int flags)
1785 * Check access permissions.
1788 access(struct access_args *uap)
1790 struct nlookupdata nd;
1793 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
1795 error = kern_access(&nd, uap->flags);
1801 kern_stat(struct nlookupdata *nd, struct stat *st)
1807 if ((error = nlookup(nd)) != 0)
1810 if ((vp = nd->nl_ncp->nc_vp) == NULL)
1814 if ((error = vget(vp, LK_SHARED, td)) != 0)
1816 error = vn_stat(vp, st, td);
1819 * If the file handle is stale we have to re-resolve the entry. This
1820 * is a hack at the moment.
1822 if (error == ESTALE) {
1823 cache_setunresolved(nd->nl_ncp);
1824 error = cache_resolve(nd->nl_ncp, nd->nl_cred);
1835 * stat_args(char *path, struct stat *ub)
1837 * Get file status; this version follows links.
1840 stat(struct stat_args *uap)
1842 struct nlookupdata nd;
1846 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
1848 error = kern_stat(&nd, &st);
1850 error = copyout(&st, uap->ub, sizeof(*uap->ub));
1857 * lstat_args(char *path, struct stat *ub)
1859 * Get file status; this version does not follow links.
1862 lstat(struct lstat_args *uap)
1864 struct nlookupdata nd;
1868 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, 0);
1870 error = kern_stat(&nd, &st);
1872 error = copyout(&st, uap->ub, sizeof(*uap->ub));
1883 nsb->st_dev = sb->st_dev;
1884 nsb->st_ino = sb->st_ino;
1885 nsb->st_mode = sb->st_mode;
1886 nsb->st_nlink = sb->st_nlink;
1887 nsb->st_uid = sb->st_uid;
1888 nsb->st_gid = sb->st_gid;
1889 nsb->st_rdev = sb->st_rdev;
1890 nsb->st_atimespec = sb->st_atimespec;
1891 nsb->st_mtimespec = sb->st_mtimespec;
1892 nsb->st_ctimespec = sb->st_ctimespec;
1893 nsb->st_size = sb->st_size;
1894 nsb->st_blocks = sb->st_blocks;
1895 nsb->st_blksize = sb->st_blksize;
1896 nsb->st_flags = sb->st_flags;
1897 nsb->st_gen = sb->st_gen;
1898 nsb->st_qspare[0] = sb->st_qspare[0];
1899 nsb->st_qspare[1] = sb->st_qspare[1];
1903 * nstat_args(char *path, struct nstat *ub)
1907 nstat(struct nstat_args *uap)
1909 struct thread *td = curthread;
1913 struct nlookupdata nd;
1917 error = nlookup_init(&nd, SCARG(uap, path), UIO_USERSPACE, NLC_FOLLOW);
1919 error = nlookup(&nd);
1921 error = cache_vget(nd.nl_ncp, nd.nl_cred, LK_EXCLUSIVE, &vp);
1924 error = vn_stat(vp, &sb, td);
1927 cvtnstat(&sb, &nsb);
1928 error = copyout(&nsb, SCARG(uap, ub), sizeof(nsb));
1935 * lstat_args(char *path, struct stat *ub)
1937 * Get file status; this version does not follow links.
1941 nlstat(struct nlstat_args *uap)
1943 struct thread *td = curthread;
1947 struct nlookupdata nd;
1951 error = nlookup_init(&nd, SCARG(uap, path), UIO_USERSPACE, 0);
1953 error = nlookup(&nd);
1955 error = cache_vget(nd.nl_ncp, nd.nl_cred, LK_EXCLUSIVE, &vp);
1958 error = vn_stat(vp, &sb, td);
1961 cvtnstat(&sb, &nsb);
1962 error = copyout(&nsb, SCARG(uap, ub), sizeof(nsb));
1969 * pathconf_Args(char *path, int name)
1971 * Get configurable pathname variables.
1975 pathconf(struct pathconf_args *uap)
1977 struct nlookupdata nd;
1982 error = nlookup_init(&nd, SCARG(uap, path), UIO_USERSPACE, NLC_FOLLOW);
1984 error = nlookup(&nd);
1986 error = cache_vget(nd.nl_ncp, nd.nl_cred, LK_EXCLUSIVE, &vp);
1989 error = VOP_PATHCONF(vp, SCARG(uap, name), uap->sysmsg_fds);
1997 * kern_readlink isn't properly split yet. There is a copyin burried
1998 * in VOP_READLINK().
2001 kern_readlink(struct nlookupdata *nd, char *buf, int count, int *res)
2003 struct thread *td = curthread;
2004 struct proc *p = td->td_proc;
2010 if ((error = nlookup(nd)) != 0)
2012 error = cache_vget(nd->nl_ncp, nd->nl_cred, LK_EXCLUSIVE, &vp);
2015 if (vp->v_type != VLNK) {
2018 aiov.iov_base = buf;
2019 aiov.iov_len = count;
2020 auio.uio_iov = &aiov;
2021 auio.uio_iovcnt = 1;
2022 auio.uio_offset = 0;
2023 auio.uio_rw = UIO_READ;
2024 auio.uio_segflg = UIO_USERSPACE;
2026 auio.uio_resid = count;
2027 error = VOP_READLINK(vp, &auio, p->p_ucred);
2030 *res = count - auio.uio_resid;
2035 * readlink_args(char *path, char *buf, int count)
2037 * Return target name of a symbolic link.
2040 readlink(struct readlink_args *uap)
2042 struct nlookupdata nd;
2045 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, 0);
2047 error = kern_readlink(&nd, uap->buf, uap->count,
2048 &uap->sysmsg_result);
2055 setfflags(struct vnode *vp, int flags)
2057 struct thread *td = curthread;
2058 struct proc *p = td->td_proc;
2063 * Prevent non-root users from setting flags on devices. When
2064 * a device is reused, users can retain ownership of the device
2065 * if they are allowed to set flags and programs assume that
2066 * chown can't fail when done as root.
2068 if ((vp->v_type == VCHR || vp->v_type == VBLK) &&
2069 ((error = suser_cred(p->p_ucred, PRISON_ROOT)) != 0))
2073 * note: vget is required for any operation that might mod the vnode
2074 * so VINACTIVE is properly cleared.
2076 VOP_LEASE(vp, td, p->p_ucred, LEASE_WRITE);
2077 if ((error = vget(vp, LK_EXCLUSIVE, td)) == 0) {
2079 vattr.va_flags = flags;
2080 error = VOP_SETATTR(vp, &vattr, p->p_ucred, td);
2087 * chflags(char *path, int flags)
2089 * Change flags of a file given a path name.
2093 chflags(struct chflags_args *uap)
2095 struct nlookupdata nd;
2100 error = nlookup_init(&nd, SCARG(uap, path), UIO_USERSPACE, NLC_FOLLOW);
2101 /* XXX Add NLC flag indicating modifying operation? */
2103 error = nlookup(&nd);
2105 error = cache_vref(nd.nl_ncp, nd.nl_cred, &vp);
2108 error = setfflags(vp, SCARG(uap, flags));
2115 * fchflags_args(int fd, int flags)
2117 * Change flags of a file given a file descriptor.
2121 fchflags(struct fchflags_args *uap)
2123 struct thread *td = curthread;
2124 struct proc *p = td->td_proc;
2128 if ((error = getvnode(p->p_fd, SCARG(uap, fd), &fp)) != 0)
2130 return setfflags((struct vnode *) fp->f_data, SCARG(uap, flags));
2134 setfmode(struct vnode *vp, int mode)
2136 struct thread *td = curthread;
2137 struct proc *p = td->td_proc;
2142 * note: vget is required for any operation that might mod the vnode
2143 * so VINACTIVE is properly cleared.
2145 VOP_LEASE(vp, td, p->p_ucred, LEASE_WRITE);
2146 if ((error = vget(vp, LK_EXCLUSIVE, td)) == 0) {
2148 vattr.va_mode = mode & ALLPERMS;
2149 error = VOP_SETATTR(vp, &vattr, p->p_ucred, td);
2156 kern_chmod(struct nlookupdata *nd, int mode)
2161 /* XXX Add NLC flag indicating modifying operation? */
2162 if ((error = nlookup(nd)) != 0)
2164 if ((error = cache_vref(nd->nl_ncp, nd->nl_cred, &vp)) != 0)
2166 error = setfmode(vp, mode);
2172 * chmod_args(char *path, int mode)
2174 * Change mode of a file given path name.
2178 chmod(struct chmod_args *uap)
2180 struct nlookupdata nd;
2183 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
2185 error = kern_chmod(&nd, uap->mode);
2191 * lchmod_args(char *path, int mode)
2193 * Change mode of a file given path name (don't follow links.)
2197 lchmod(struct lchmod_args *uap)
2199 struct nlookupdata nd;
2202 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, 0);
2204 error = kern_chmod(&nd, uap->mode);
2210 * fchmod_args(int fd, int mode)
2212 * Change mode of a file given a file descriptor.
2216 fchmod(struct fchmod_args *uap)
2218 struct thread *td = curthread;
2219 struct proc *p = td->td_proc;
2223 if ((error = getvnode(p->p_fd, SCARG(uap, fd), &fp)) != 0)
2225 return setfmode((struct vnode *)fp->f_data, SCARG(uap, mode));
2229 setfown(struct vnode *vp, uid_t uid, gid_t gid)
2231 struct thread *td = curthread;
2232 struct proc *p = td->td_proc;
2237 * note: vget is required for any operation that might mod the vnode
2238 * so VINACTIVE is properly cleared.
2240 VOP_LEASE(vp, td, p->p_ucred, LEASE_WRITE);
2241 if ((error = vget(vp, LK_EXCLUSIVE, td)) == 0) {
2245 error = VOP_SETATTR(vp, &vattr, p->p_ucred, td);
2252 kern_chown(struct nlookupdata *nd, int uid, int gid)
2257 /* XXX Add NLC flag indicating modifying operation? */
2258 if ((error = nlookup(nd)) != 0)
2260 if ((error = cache_vref(nd->nl_ncp, nd->nl_cred, &vp)) != 0)
2262 error = setfown(vp, uid, gid);
2268 * chown(char *path, int uid, int gid)
2270 * Set ownership given a path name.
2273 chown(struct chown_args *uap)
2275 struct nlookupdata nd;
2278 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
2280 error = kern_chown(&nd, uap->uid, uap->gid);
2286 * lchown_args(char *path, int uid, int gid)
2288 * Set ownership given a path name, do not cross symlinks.
2291 lchown(struct lchown_args *uap)
2293 struct nlookupdata nd;
2296 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, 0);
2298 error = kern_chown(&nd, uap->uid, uap->gid);
2304 * fchown_args(int fd, int uid, int gid)
2306 * Set ownership given a file descriptor.
2310 fchown(struct fchown_args *uap)
2312 struct thread *td = curthread;
2313 struct proc *p = td->td_proc;
2317 if ((error = getvnode(p->p_fd, SCARG(uap, fd), &fp)) != 0)
2319 return setfown((struct vnode *)fp->f_data,
2320 SCARG(uap, uid), SCARG(uap, gid));
2324 getutimes(const struct timeval *tvp, struct timespec *tsp)
2326 struct timeval tv[2];
2330 TIMEVAL_TO_TIMESPEC(&tv[0], &tsp[0]);
2333 TIMEVAL_TO_TIMESPEC(&tvp[0], &tsp[0]);
2334 TIMEVAL_TO_TIMESPEC(&tvp[1], &tsp[1]);
2340 setutimes(struct vnode *vp, const struct timespec *ts, int nullflag)
2342 struct thread *td = curthread;
2343 struct proc *p = td->td_proc;
2348 * note: vget is required for any operation that might mod the vnode
2349 * so VINACTIVE is properly cleared.
2351 VOP_LEASE(vp, td, p->p_ucred, LEASE_WRITE);
2352 if ((error = vget(vp, LK_EXCLUSIVE, td)) == 0) {
2354 vattr.va_atime = ts[0];
2355 vattr.va_mtime = ts[1];
2357 vattr.va_vaflags |= VA_UTIMES_NULL;
2358 error = VOP_SETATTR(vp, &vattr, p->p_ucred, td);
2365 kern_utimes(struct nlookupdata *nd, struct timeval *tptr)
2367 struct timespec ts[2];
2371 if ((error = getutimes(tptr, ts)) != 0)
2373 /* XXX Add NLC flag indicating modifying operation? */
2374 if ((error = nlookup(nd)) != 0)
2376 if ((error = cache_vref(nd->nl_ncp, nd->nl_cred, &vp)) != 0)
2378 error = setutimes(vp, ts, tptr == NULL);
2384 * utimes_args(char *path, struct timeval *tptr)
2386 * Set the access and modification times of a file.
2389 utimes(struct utimes_args *uap)
2391 struct timeval tv[2];
2392 struct nlookupdata nd;
2396 error = copyin(uap->tptr, tv, sizeof(tv));
2400 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
2402 error = kern_utimes(&nd, uap->tptr ? tv : NULL);
2408 * lutimes_args(char *path, struct timeval *tptr)
2410 * Set the access and modification times of a file.
2413 lutimes(struct lutimes_args *uap)
2415 struct timeval tv[2];
2416 struct nlookupdata nd;
2420 error = copyin(uap->tptr, tv, sizeof(tv));
2424 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, 0);
2426 error = kern_utimes(&nd, uap->tptr ? tv : NULL);
2432 kern_futimes(int fd, struct timeval *tptr)
2434 struct thread *td = curthread;
2435 struct proc *p = td->td_proc;
2436 struct timespec ts[2];
2440 error = getutimes(tptr, ts);
2443 error = getvnode(p->p_fd, fd, &fp);
2446 error = setutimes((struct vnode *)fp->f_data, ts, tptr == NULL);
2451 * futimes_args(int fd, struct timeval *tptr)
2453 * Set the access and modification times of a file.
2456 futimes(struct futimes_args *uap)
2458 struct timeval tv[2];
2462 error = copyin(uap->tptr, tv, sizeof(tv));
2467 error = kern_futimes(uap->fd, uap->tptr ? tv : NULL);
2473 kern_truncate(struct nlookupdata *nd, off_t length)
2481 /* XXX Add NLC flag indicating modifying operation? */
2482 if ((error = nlookup(nd)) != 0)
2484 if ((error = cache_vref(nd->nl_ncp, nd->nl_cred, &vp)) != 0)
2486 VOP_LEASE(vp, nd->nl_td, nd->nl_cred, LEASE_WRITE);
2487 if ((error = vn_lock(vp, LK_EXCLUSIVE | LK_RETRY, nd->nl_td)) != 0) {
2491 if (vp->v_type == VDIR) {
2493 } else if ((error = vn_writechk(vp)) == 0 &&
2494 (error = VOP_ACCESS(vp, VWRITE, nd->nl_cred, nd->nl_td)) == 0) {
2496 vattr.va_size = length;
2497 error = VOP_SETATTR(vp, &vattr, nd->nl_cred, nd->nl_td);
2504 * truncate(char *path, int pad, off_t length)
2506 * Truncate a file given its path name.
2509 truncate(struct truncate_args *uap)
2511 struct nlookupdata nd;
2514 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
2516 error = kern_truncate(&nd, uap->length);
2522 kern_ftruncate(int fd, off_t length)
2524 struct thread *td = curthread;
2525 struct proc *p = td->td_proc;
2533 if ((error = getvnode(p->p_fd, fd, &fp)) != 0)
2535 if ((fp->f_flag & FWRITE) == 0)
2537 vp = (struct vnode *)fp->f_data;
2538 VOP_LEASE(vp, td, p->p_ucred, LEASE_WRITE);
2539 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY, td);
2540 if (vp->v_type == VDIR)
2542 else if ((error = vn_writechk(vp)) == 0) {
2544 vattr.va_size = length;
2545 error = VOP_SETATTR(vp, &vattr, fp->f_cred, td);
2547 VOP_UNLOCK(vp, 0, td);
2552 * ftruncate_args(int fd, int pad, off_t length)
2554 * Truncate a file given a file descriptor.
2557 ftruncate(struct ftruncate_args *uap)
2561 error = kern_ftruncate(uap->fd, uap->length);
2569 * Sync an open file.
2573 fsync(struct fsync_args *uap)
2575 struct thread *td = curthread;
2576 struct proc *p = td->td_proc;
2582 if ((error = getvnode(p->p_fd, SCARG(uap, fd), &fp)) != 0)
2584 vp = (struct vnode *)fp->f_data;
2585 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY, td);
2586 if (VOP_GETVOBJECT(vp, &obj) == 0)
2587 vm_object_page_clean(obj, 0, 0, 0);
2588 if ((error = VOP_FSYNC(vp, MNT_WAIT, td)) == 0 &&
2589 vp->v_mount && (vp->v_mount->mnt_flag & MNT_SOFTDEP) &&
2591 error = (*bioops.io_fsync)(vp);
2592 VOP_UNLOCK(vp, 0, td);
2597 kern_rename(struct nlookupdata *fromnd, struct nlookupdata *tond)
2599 struct namecache *fncpd;
2600 struct namecache *tncpd;
2601 struct namecache *ncp;
2606 if ((error = nlookup(fromnd)) != 0)
2608 if ((fncpd = fromnd->nl_ncp->nc_parent) == NULL)
2613 * unlock the source ncp so we can lookup the target ncp without
2614 * deadlocking. The target may or may not exist so we do not check
2615 * for a target vp like kern_mkdir() and other creation functions do.
2617 * The source and target directories are ref'd and rechecked after
2618 * everything is relocked to determine if the source or target file
2621 KKASSERT(fromnd->nl_flags & NLC_NCPISLOCKED);
2622 fromnd->nl_flags &= ~NLC_NCPISLOCKED;
2623 cache_unlock(fromnd->nl_ncp);
2625 tond->nl_flags |= NLC_CREATE;
2626 if ((error = nlookup(tond)) != 0) {
2630 if ((tncpd = tond->nl_ncp->nc_parent) == NULL) {
2637 * If the source and target are the same there is nothing to do
2639 if (fromnd->nl_ncp == tond->nl_ncp) {
2646 * relock the source ncp
2648 if (cache_lock_nonblock(fromnd->nl_ncp) == 0) {
2649 cache_resolve(fromnd->nl_ncp, fromnd->nl_cred);
2650 } else if (fromnd->nl_ncp > tond->nl_ncp) {
2651 cache_lock(fromnd->nl_ncp);
2652 cache_resolve(fromnd->nl_ncp, fromnd->nl_cred);
2654 cache_unlock(tond->nl_ncp);
2655 cache_lock(fromnd->nl_ncp);
2656 cache_resolve(fromnd->nl_ncp, fromnd->nl_cred);
2657 cache_lock(tond->nl_ncp);
2658 cache_resolve(tond->nl_ncp, tond->nl_cred);
2660 fromnd->nl_flags |= NLC_NCPISLOCKED;
2663 * make sure the parent directories linkages are the same
2665 if (fncpd != fromnd->nl_ncp->nc_parent ||
2666 tncpd != tond->nl_ncp->nc_parent) {
2673 * Both the source and target must be within the same filesystem and
2674 * in the same filesystem as their parent directories within the
2675 * namecache topology.
2677 mp = fncpd->nc_mount;
2678 if (mp != tncpd->nc_mount || mp != fromnd->nl_ncp->nc_mount ||
2679 mp != tond->nl_ncp->nc_mount) {
2686 * If the target exists and either the source or target is a directory,
2687 * then both must be directories.
2689 if (tond->nl_ncp->nc_vp) {
2690 if (fromnd->nl_ncp->nc_vp->v_type == VDIR) {
2691 if (tond->nl_ncp->nc_vp->v_type != VDIR)
2693 } else if (tond->nl_ncp->nc_vp->v_type == VDIR) {
2699 * You cannot rename a source into itself or a subdirectory of itself.
2700 * We check this by travsersing the target directory upwards looking
2701 * for a match against the source.
2704 for (ncp = tncpd; ncp; ncp = ncp->nc_parent) {
2705 if (fromnd->nl_ncp == ncp) {
2716 error = VOP_NRENAME(fromnd->nl_ncp, tond->nl_ncp, tond->nl_cred);
2721 * rename_args(char *from, char *to)
2723 * Rename files. Source and destination must either both be directories,
2724 * or both not be directories. If target is a directory, it must be empty.
2727 rename(struct rename_args *uap)
2729 struct nlookupdata fromnd, tond;
2732 error = nlookup_init(&fromnd, uap->from, UIO_USERSPACE, 0);
2734 error = nlookup_init(&tond, uap->to, UIO_USERSPACE, 0);
2736 error = kern_rename(&fromnd, &tond);
2737 nlookup_done(&tond);
2739 nlookup_done(&fromnd);
2744 kern_mkdir(struct nlookupdata *nd, int mode)
2746 struct thread *td = curthread;
2747 struct proc *p = td->td_proc;
2748 struct namecache *ncp;
2754 nd->nl_flags |= NLC_WILLBEDIR | NLC_CREATE;
2755 if ((error = nlookup(nd)) != 0)
2763 vattr.va_type = VDIR;
2764 vattr.va_mode = (mode & ACCESSPERMS) &~ p->p_fd->fd_cmask;
2767 error = VOP_NMKDIR(ncp, &vp, p->p_ucred, &vattr);
2774 * mkdir_args(char *path, int mode)
2776 * Make a directory file.
2780 mkdir(struct mkdir_args *uap)
2782 struct nlookupdata nd;
2785 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, 0);
2787 error = kern_mkdir(&nd, uap->mode);
2793 kern_rmdir(struct nlookupdata *nd)
2795 struct namecache *ncp;
2799 nd->nl_flags |= NLC_DELETE;
2800 if ((error = nlookup(nd)) != 0)
2804 error = VOP_NRMDIR(ncp, nd->nl_cred);
2809 * rmdir_args(char *path)
2811 * Remove a directory file.
2815 rmdir(struct rmdir_args *uap)
2817 struct nlookupdata nd;
2820 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, 0);
2822 error = kern_rmdir(&nd);
2828 kern_getdirentries(int fd, char *buf, u_int count, long *basep, int *res)
2830 struct thread *td = curthread;
2831 struct proc *p = td->td_proc;
2839 if ((error = getvnode(p->p_fd, fd, &fp)) != 0)
2841 if ((fp->f_flag & FREAD) == 0)
2843 vp = (struct vnode *)fp->f_data;
2845 if (vp->v_type != VDIR)
2847 aiov.iov_base = buf;
2848 aiov.iov_len = count;
2849 auio.uio_iov = &aiov;
2850 auio.uio_iovcnt = 1;
2851 auio.uio_rw = UIO_READ;
2852 auio.uio_segflg = UIO_USERSPACE;
2854 auio.uio_resid = count;
2855 /* vn_lock(vp, LK_SHARED | LK_RETRY, td); */
2856 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY, td);
2857 loff = auio.uio_offset = fp->f_offset;
2858 error = VOP_READDIR(vp, &auio, fp->f_cred, &eofflag, NULL, NULL);
2859 fp->f_offset = auio.uio_offset;
2860 VOP_UNLOCK(vp, 0, td);
2863 if (count == auio.uio_resid) {
2864 if (union_dircheckp) {
2865 error = union_dircheckp(td, &vp, fp);
2871 if ((vp->v_flag & VROOT) &&
2872 (vp->v_mount->mnt_flag & MNT_UNION)) {
2873 struct vnode *tvp = vp;
2874 vp = vp->v_mount->mnt_vnodecovered;
2876 fp->f_data = (caddr_t)vp;
2885 *res = count - auio.uio_resid;
2890 * getdirentries_args(int fd, char *buf, u_int conut, long *basep)
2892 * Read a block of directory entries in a file system independent format.
2895 getdirentries(struct getdirentries_args *uap)
2900 error = kern_getdirentries(uap->fd, uap->buf, uap->count, &base,
2901 &uap->sysmsg_result);
2904 error = copyout(&base, uap->basep, sizeof(*uap->basep));
2909 * getdents_args(int fd, char *buf, size_t count)
2912 getdents(struct getdents_args *uap)
2916 error = kern_getdirentries(uap->fd, uap->buf, uap->count, NULL,
2917 &uap->sysmsg_result);
2923 * umask(int newmask)
2925 * Set the mode mask for creation of filesystem nodes.
2930 umask(struct umask_args *uap)
2932 struct thread *td = curthread;
2933 struct proc *p = td->td_proc;
2934 struct filedesc *fdp;
2937 uap->sysmsg_result = fdp->fd_cmask;
2938 fdp->fd_cmask = SCARG(uap, newmask) & ALLPERMS;
2943 * revoke(char *path)
2945 * Void all references to file by ripping underlying filesystem
2950 revoke(struct revoke_args *uap)
2952 struct thread *td = curthread;
2953 struct nlookupdata nd;
2960 error = nlookup_init(&nd, SCARG(uap, path), UIO_USERSPACE, NLC_FOLLOW);
2962 error = nlookup(&nd);
2964 error = cache_vref(nd.nl_ncp, nd.nl_cred, &vp);
2965 cred = crhold(nd.nl_cred);
2968 if (vp->v_type != VCHR && vp->v_type != VBLK)
2971 error = VOP_GETATTR(vp, &vattr, td);
2972 if (error == 0 && cred->cr_uid != vattr.va_uid)
2973 error = suser_cred(cred, PRISON_ROOT);
2974 if (error == 0 && count_udev(vp->v_udev) > 0) {
2975 if ((error = vx_lock(vp)) == 0) {
2976 VOP_REVOKE(vp, REVOKEALL);
2987 * Convert a user file descriptor to a kernel file entry.
2990 getvnode(struct filedesc *fdp, int fd, struct file **fpp)
2994 if ((u_int)fd >= fdp->fd_nfiles ||
2995 (fp = fdp->fd_ofiles[fd]) == NULL)
2997 if (fp->f_type != DTYPE_VNODE && fp->f_type != DTYPE_FIFO)
3003 * getfh_args(char *fname, fhandle_t *fhp)
3005 * Get (NFS) file handle
3008 getfh(struct getfh_args *uap)
3010 struct thread *td = curthread;
3011 struct nlookupdata nd;
3017 * Must be super user
3019 if ((error = suser(td)) != 0)
3023 error = nlookup_init(&nd, uap->fname, UIO_USERSPACE, NLC_FOLLOW);
3025 error = nlookup(&nd);
3027 error = cache_vget(nd.nl_ncp, nd.nl_cred, LK_EXCLUSIVE, &vp);
3030 bzero(&fh, sizeof(fh));
3031 fh.fh_fsid = vp->v_mount->mnt_stat.f_fsid;
3032 error = VFS_VPTOFH(vp, &fh.fh_fid);
3035 error = copyout(&fh, uap->fhp, sizeof(fh));
3041 * fhopen_args(const struct fhandle *u_fhp, int flags)
3043 * syscall for the rpc.lockd to use to translate a NFS file handle into
3044 * an open descriptor.
3046 * warning: do not remove the suser() call or this becomes one giant
3050 fhopen(struct fhopen_args *uap)
3052 struct thread *td = curthread;
3053 struct proc *p = td->td_proc;
3058 struct vattr *vap = &vat;
3060 struct filedesc *fdp = p->p_fd;
3061 int fmode, mode, error, type;
3067 * Must be super user
3073 fmode = FFLAGS(SCARG(uap, flags));
3074 /* why not allow a non-read/write open for our lockd? */
3075 if (((fmode & (FREAD | FWRITE)) == 0) || (fmode & O_CREAT))
3077 error = copyin(SCARG(uap,u_fhp), &fhp, sizeof(fhp));
3080 /* find the mount point */
3081 mp = vfs_getvfs(&fhp.fh_fsid);
3084 /* now give me my vnode, it gets returned to me locked */
3085 error = VFS_FHTOVP(mp, &fhp.fh_fid, &vp);
3089 * from now on we have to make sure not
3090 * to forget about the vnode
3091 * any error that causes an abort must vput(vp)
3092 * just set error = err and 'goto bad;'.
3098 if (vp->v_type == VLNK) {
3102 if (vp->v_type == VSOCK) {
3107 if (fmode & (FWRITE | O_TRUNC)) {
3108 if (vp->v_type == VDIR) {
3112 error = vn_writechk(vp);
3120 error = VOP_ACCESS(vp, mode, p->p_ucred, td);
3124 if (fmode & O_TRUNC) {
3125 VOP_UNLOCK(vp, 0, td); /* XXX */
3126 VOP_LEASE(vp, td, p->p_ucred, LEASE_WRITE);
3127 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY, td); /* XXX */
3130 error = VOP_SETATTR(vp, vap, p->p_ucred, td);
3136 * VOP_OPEN needs the file pointer so it can potentially override
3139 * WARNING! no f_ncp will be associated when fhopen()ing a directory.
3142 if ((error = falloc(p, &nfp, NULL)) != 0)
3146 fp->f_data = (caddr_t)vp;
3147 fp->f_flag = fmode & FMASK;
3148 fp->f_ops = &vnode_fileops;
3149 fp->f_type = DTYPE_VNODE;
3151 error = VOP_OPEN(vp, fmode, p->p_ucred, fp, td);
3154 * setting f_ops this way prevents VOP_CLOSE from being
3155 * called or fdrop() releasing the vp from v_data. Since
3156 * the VOP_OPEN failed we don't want to VOP_CLOSE.
3158 fp->f_ops = &badfileops;
3167 * The fp now owns a reference on the vnode. We still have our own
3173 * Make sure that a VM object is created for VMIO support. If this
3174 * fails just fdrop() normally to clean up.
3176 if (vn_canvmio(vp) == TRUE) {
3177 if ((error = vfs_object_create(vp, td)) != 0) {
3184 * The open was successful, associate it with a file descriptor.
3186 if ((error = fsetfd(p, fp, &indx)) != 0) {
3193 if (fmode & (O_EXLOCK | O_SHLOCK)) {
3194 lf.l_whence = SEEK_SET;
3197 if (fmode & O_EXLOCK)
3198 lf.l_type = F_WRLCK;
3200 lf.l_type = F_RDLCK;
3202 if ((fmode & FNONBLOCK) == 0)
3204 VOP_UNLOCK(vp, 0, td);
3205 if ((error = VOP_ADVLOCK(vp, (caddr_t)fp, F_SETLK, &lf, type)) != 0) {
3207 * lock request failed. Normally close the descriptor
3208 * but handle the case where someone might have dup()d
3209 * or close()d it when we weren't looking.
3211 if (fdp->fd_ofiles[indx] == fp) {
3212 fdp->fd_ofiles[indx] = NULL;
3217 * release our private reference.
3223 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY, td);
3224 fp->f_flag |= FHASLOCK;
3226 if ((vp->v_type == VREG) && (VOP_GETVOBJECT(vp, NULL) != 0))
3227 vfs_object_create(vp, td);
3231 uap->sysmsg_result = indx;
3240 * fhstat_args(struct fhandle *u_fhp, struct stat *sb)
3243 fhstat(struct fhstat_args *uap)
3245 struct thread *td = curthread;
3253 * Must be super user
3259 error = copyin(SCARG(uap, u_fhp), &fh, sizeof(fhandle_t));
3263 if ((mp = vfs_getvfs(&fh.fh_fsid)) == NULL)
3265 if ((error = VFS_FHTOVP(mp, &fh.fh_fid, &vp)))
3267 error = vn_stat(vp, &sb, td);
3271 error = copyout(&sb, SCARG(uap, sb), sizeof(sb));
3276 * fhstatfs_args(struct fhandle *u_fhp, struct statfs *buf)
3279 fhstatfs(struct fhstatfs_args *uap)
3281 struct thread *td = curthread;
3290 * Must be super user
3292 if ((error = suser(td)))
3295 if ((error = copyin(SCARG(uap, u_fhp), &fh, sizeof(fhandle_t))) != 0)
3298 if ((mp = vfs_getvfs(&fh.fh_fsid)) == NULL)
3300 if ((error = VFS_FHTOVP(mp, &fh.fh_fid, &vp)))
3305 if ((error = VFS_STATFS(mp, sp, td)) != 0)
3307 sp->f_flags = mp->mnt_flag & MNT_VISFLAGMASK;
3309 bcopy(sp, &sb, sizeof(sb));
3310 sb.f_fsid.val[0] = sb.f_fsid.val[1] = 0;
3313 return (copyout(sp, SCARG(uap, buf), sizeof(*sp)));
3317 * Syscall to push extended attribute configuration information into the
3318 * VFS. Accepts a path, which it converts to a mountpoint, as well as
3319 * a command (int cmd), and attribute name and misc data. For now, the
3320 * attribute name is left in userspace for consumption by the VFS_op.
3321 * It will probably be changed to be copied into sysspace by the
3322 * syscall in the future, once issues with various consumers of the
3323 * attribute code have raised their hands.
3325 * Currently this is used only by UFS Extended Attributes.
3328 extattrctl(struct extattrctl_args *uap)
3330 struct nlookupdata nd;
3336 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
3338 error = nlookup(&nd);
3340 mp = nd.nl_ncp->nc_mount;
3341 error = VFS_EXTATTRCTL(mp, SCARG(uap, cmd),
3342 SCARG(uap, attrname), SCARG(uap, arg),
3350 * Syscall to set a named extended attribute on a file or directory.
3351 * Accepts attribute name, and a uio structure pointing to the data to set.
3352 * The uio is consumed in the style of writev(). The real work happens
3353 * in VOP_SETEXTATTR().
3356 extattr_set_file(struct extattr_set_file_args *uap)
3358 char attrname[EXTATTR_MAXNAMELEN];
3359 struct iovec aiov[UIO_SMALLIOV];
3360 struct iovec *needfree;
3361 struct nlookupdata nd;
3370 error = copyin(SCARG(uap, attrname), attrname, EXTATTR_MAXNAMELEN);
3375 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
3377 error = nlookup(&nd);
3379 error = cache_vget(nd.nl_ncp, nd.nl_cred, LK_EXCLUSIVE, &vp);
3386 iovlen = uap->iovcnt * sizeof(struct iovec);
3387 if (uap->iovcnt > UIO_SMALLIOV) {
3388 if (uap->iovcnt > UIO_MAXIOV) {
3392 MALLOC(iov, struct iovec *, iovlen, M_IOV, M_WAITOK);
3398 auio.uio_iovcnt = uap->iovcnt;
3399 auio.uio_rw = UIO_WRITE;
3400 auio.uio_segflg = UIO_USERSPACE;
3401 auio.uio_td = nd.nl_td;
3402 auio.uio_offset = 0;
3403 if ((error = copyin(uap->iovp, iov, iovlen)))
3406 for (i = 0; i < uap->iovcnt; i++) {
3407 if (iov->iov_len > INT_MAX - auio.uio_resid) {
3411 auio.uio_resid += iov->iov_len;
3414 cnt = auio.uio_resid;
3415 error = VOP_SETEXTATTR(vp, attrname, &auio, nd.nl_cred, nd.nl_td);
3416 cnt -= auio.uio_resid;
3417 uap->sysmsg_result = cnt;
3422 FREE(needfree, M_IOV);
3427 * Syscall to get a named extended attribute on a file or directory.
3428 * Accepts attribute name, and a uio structure pointing to a buffer for the
3429 * data. The uio is consumed in the style of readv(). The real work
3430 * happens in VOP_GETEXTATTR();
3433 extattr_get_file(struct extattr_get_file_args *uap)
3435 char attrname[EXTATTR_MAXNAMELEN];
3436 struct iovec aiov[UIO_SMALLIOV];
3437 struct iovec *needfree;
3438 struct nlookupdata nd;
3447 error = copyin(SCARG(uap, attrname), attrname, EXTATTR_MAXNAMELEN);
3452 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
3454 error = nlookup(&nd);
3456 error = cache_vget(nd.nl_ncp, nd.nl_cred, LK_EXCLUSIVE, &vp);
3462 iovlen = uap->iovcnt * sizeof (struct iovec);
3464 if (uap->iovcnt > UIO_SMALLIOV) {
3465 if (uap->iovcnt > UIO_MAXIOV) {
3469 MALLOC(iov, struct iovec *, iovlen, M_IOV, M_WAITOK);
3475 auio.uio_iovcnt = uap->iovcnt;
3476 auio.uio_rw = UIO_READ;
3477 auio.uio_segflg = UIO_USERSPACE;
3478 auio.uio_td = nd.nl_td;
3479 auio.uio_offset = 0;
3480 if ((error = copyin(uap->iovp, iov, iovlen)))
3483 for (i = 0; i < uap->iovcnt; i++) {
3484 if (iov->iov_len > INT_MAX - auio.uio_resid) {
3488 auio.uio_resid += iov->iov_len;
3491 cnt = auio.uio_resid;
3492 error = VOP_GETEXTATTR(vp, attrname, &auio, nd.nl_cred, nd.nl_td);
3493 cnt -= auio.uio_resid;
3494 uap->sysmsg_result = cnt;
3499 FREE(needfree, M_IOV);
3504 * Syscall to delete a named extended attribute from a file or directory.
3505 * Accepts attribute name. The real work happens in VOP_SETEXTATTR().
3508 extattr_delete_file(struct extattr_delete_file_args *uap)
3510 char attrname[EXTATTR_MAXNAMELEN];
3511 struct nlookupdata nd;
3515 error = copyin(SCARG(uap, attrname), attrname, EXTATTR_MAXNAMELEN);
3520 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
3522 error = nlookup(&nd);
3524 error = cache_vget(nd.nl_ncp, nd.nl_cred, LK_EXCLUSIVE, &vp);
3530 error = VOP_SETEXTATTR(vp, attrname, NULL, nd.nl_cred, nd.nl_td);
3537 * print out statistics from the current status of the buffer pool
3538 * this can be toggeled by the system control option debug.syncprt
3547 int counts[(MAXBSIZE / PAGE_SIZE) + 1];
3548 static char *bname[3] = { "LOCKED", "LRU", "AGE" };
3550 for (dp = bufqueues, i = 0; dp < &bufqueues[3]; dp++, i++) {
3552 for (j = 0; j <= MAXBSIZE/PAGE_SIZE; j++)
3555 TAILQ_FOREACH(bp, dp, b_freelist) {
3556 counts[bp->b_bufsize/PAGE_SIZE]++;
3560 printf("%s: total-%d", bname[i], count);
3561 for (j = 0; j <= MAXBSIZE/PAGE_SIZE; j++)
3563 printf(", %d-%d", j * PAGE_SIZE, counts[j]);