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.46 2004/11/12 00:09:24 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/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 mp->mnt_nvnodelistsize = 0;
294 lockinit(&mp->mnt_lock, 0, "vfslock", 0, LK_NOPAUSE);
295 vfs_busy(mp, LK_NOWAIT, NULL, td);
296 mp->mnt_op = vfsp->vfc_vfsops;
298 vfsp->vfc_refcount++;
299 mp->mnt_stat.f_type = vfsp->vfc_typenum;
300 mp->mnt_flag |= vfsp->vfc_flags & MNT_VISFLAGMASK;
301 strncpy(mp->mnt_stat.f_fstypename, vfsp->vfc_name, MFSNAMELEN);
302 mp->mnt_vnodecovered = vp;
303 mp->mnt_stat.f_owner = p->p_ucred->cr_uid;
304 mp->mnt_iosize_max = DFLTPHYS;
305 VOP_UNLOCK(vp, 0, td);
308 * Set the mount level flags.
310 if (SCARG(uap, flags) & MNT_RDONLY)
311 mp->mnt_flag |= MNT_RDONLY;
312 else if (mp->mnt_flag & MNT_RDONLY)
313 mp->mnt_kern_flag |= MNTK_WANTRDWR;
314 mp->mnt_flag &=~ (MNT_NOSUID | MNT_NOEXEC | MNT_NODEV |
315 MNT_SYNCHRONOUS | MNT_UNION | MNT_ASYNC | MNT_NOATIME |
316 MNT_NOSYMFOLLOW | MNT_IGNORE |
317 MNT_NOCLUSTERR | MNT_NOCLUSTERW | MNT_SUIDDIR);
318 mp->mnt_flag |= SCARG(uap, flags) & (MNT_NOSUID | MNT_NOEXEC |
319 MNT_NODEV | MNT_SYNCHRONOUS | MNT_UNION | MNT_ASYNC | MNT_FORCE |
320 MNT_NOSYMFOLLOW | MNT_IGNORE |
321 MNT_NOATIME | MNT_NOCLUSTERR | MNT_NOCLUSTERW | MNT_SUIDDIR);
323 * Mount the filesystem.
324 * XXX The final recipients of VFS_MOUNT just overwrite the ndp they
327 error = VFS_MOUNT(mp, SCARG(uap, path), SCARG(uap, data), td);
328 if (mp->mnt_flag & MNT_UPDATE) {
329 if (mp->mnt_kern_flag & MNTK_WANTRDWR)
330 mp->mnt_flag &= ~MNT_RDONLY;
331 mp->mnt_flag &=~ (MNT_UPDATE | MNT_RELOAD | MNT_FORCE);
332 mp->mnt_kern_flag &=~ MNTK_WANTRDWR;
335 mp->mnt_kern_flag = flag2;
338 vp->v_flag &= ~VMOUNT;
343 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY, td);
345 * Put the new filesystem on the mount list after root. The mount
346 * point gets its own mnt_ncp which is a special ncp linking the
347 * vnode-under to the root of the new mount. The lookup code
348 * detects the mount point going forward and detects the special
349 * mnt_ncp via NCP_MOUNTPT going backwards.
351 * It is not necessary to invalidate or purge the vnode underneath
352 * because elements under the mount will be given their own glue
356 nlc.nlc_nameptr = "";
358 mp->mnt_ncp = cache_nlookup(ncp, &nlc);
359 mp->mnt_ncp->nc_flag |= NCF_MOUNTPT;
360 mp->mnt_ncp->nc_mount = mp;
362 /* XXX get the root of the fs and cache_setvp(mnt_ncp...) */
363 vp->v_flag &= ~VMOUNT;
364 vp->v_mountedhere = mp;
365 lwkt_gettoken(&ilock, &mountlist_token);
366 TAILQ_INSERT_TAIL(&mountlist, mp, mnt_list);
367 lwkt_reltoken(&ilock);
368 checkdirs(vp, mp->mnt_ncp);
369 cache_unlock(mp->mnt_ncp); /* leave ref intact */
370 VOP_UNLOCK(vp, 0, td);
371 error = vfs_allocate_syncvnode(mp);
373 if ((error = VFS_START(mp, 0, td)) != 0)
376 vfs_rm_vnodeops(&mp->mnt_vn_ops);
377 vfs_rm_vnodeops(&mp->mnt_vn_spec_ops);
378 vfs_rm_vnodeops(&mp->mnt_vn_fifo_ops);
379 vp->v_flag &= ~VMOUNT;
380 mp->mnt_vfc->vfc_refcount--;
390 * Scan all active processes to see if any of them have a current
391 * or root directory onto which the new filesystem has just been
392 * mounted. If so, replace them with the new mount point.
394 * The passed ncp is ref'd and locked (from the mount code) and
395 * must be associated with the vnode representing the root of the
399 checkdirs(struct vnode *olddp, struct namecache *ncp)
401 struct filedesc *fdp;
406 if (olddp->v_usecount == 1)
408 mp = olddp->v_mountedhere;
409 if (VFS_ROOT(mp, &newdp))
410 panic("mount: lost mount");
411 cache_setvp(ncp, newdp);
413 if (rootvnode == olddp) {
415 vfs_cache_setroot(newdp, cache_hold(ncp));
418 FOREACH_PROC_IN_SYSTEM(p) {
420 if (fdp->fd_cdir == olddp) {
423 fdp->fd_cdir = newdp;
424 cache_drop(fdp->fd_ncdir);
425 fdp->fd_ncdir = cache_hold(ncp);
427 if (fdp->fd_rdir == olddp) {
430 fdp->fd_rdir = newdp;
431 cache_drop(fdp->fd_nrdir);
432 fdp->fd_nrdir = cache_hold(ncp);
439 * Unmount a file system.
441 * Note: unmount takes a path to the vnode mounted on as argument,
442 * not special file (as before).
445 * umount_args(char *path, int flags)
449 unmount(struct unmount_args *uap)
451 struct thread *td = curthread;
452 struct proc *p = td->td_proc;
456 struct nlookupdata nd;
459 if (p->p_ucred->cr_prison != NULL)
461 if (usermount == 0 && (error = suser(td)))
465 error = nlookup_init(&nd, SCARG(uap, path), UIO_USERSPACE, NLC_FOLLOW);
467 error = nlookup(&nd);
469 error = cache_vget(nd.nl_ncp, nd.nl_cred, LK_EXCLUSIVE, &vp);
477 * Only root, or the user that did the original mount is
478 * permitted to unmount this filesystem.
480 if ((mp->mnt_stat.f_owner != p->p_ucred->cr_uid) &&
481 (error = suser(td))) {
487 * Don't allow unmounting the root file system.
489 if (mp->mnt_flag & MNT_ROOTFS) {
495 * Must be the root of the filesystem
497 if ((vp->v_flag & VROOT) == 0) {
502 return (dounmount(mp, SCARG(uap, flags), td));
506 * Do the actual file system unmount.
509 dounmount(struct mount *mp, int flags, struct thread *td)
511 struct vnode *coveredvp;
516 lwkt_gettoken(&ilock, &mountlist_token);
517 if (mp->mnt_kern_flag & MNTK_UNMOUNT) {
518 lwkt_reltoken(&ilock);
521 mp->mnt_kern_flag |= MNTK_UNMOUNT;
522 /* Allow filesystems to detect that a forced unmount is in progress. */
523 if (flags & MNT_FORCE)
524 mp->mnt_kern_flag |= MNTK_UNMOUNTF;
525 error = lockmgr(&mp->mnt_lock, LK_DRAIN | LK_INTERLOCK |
526 ((flags & MNT_FORCE) ? 0 : LK_NOWAIT), &ilock, td);
528 mp->mnt_kern_flag &= ~(MNTK_UNMOUNT | MNTK_UNMOUNTF);
529 if (mp->mnt_kern_flag & MNTK_MWAIT)
534 if (mp->mnt_flag & MNT_EXPUBLIC)
535 vfs_setpublicfs(NULL, NULL, NULL);
537 vfs_msync(mp, MNT_WAIT);
538 async_flag = mp->mnt_flag & MNT_ASYNC;
539 mp->mnt_flag &=~ MNT_ASYNC;
540 cache_purgevfs(mp); /* remove cache entries for this file sys */
541 if (mp->mnt_syncer != NULL)
542 vrele(mp->mnt_syncer);
543 if (((mp->mnt_flag & MNT_RDONLY) ||
544 (error = VFS_SYNC(mp, MNT_WAIT, td)) == 0) ||
546 error = VFS_UNMOUNT(mp, flags, td);
547 lwkt_gettokref(&ilock);
549 if (mp->mnt_syncer == NULL)
550 vfs_allocate_syncvnode(mp);
551 mp->mnt_kern_flag &= ~(MNTK_UNMOUNT | MNTK_UNMOUNTF);
552 mp->mnt_flag |= async_flag;
553 lockmgr(&mp->mnt_lock, LK_RELEASE | LK_INTERLOCK | LK_REENABLE,
555 if (mp->mnt_kern_flag & MNTK_MWAIT)
559 TAILQ_REMOVE(&mountlist, mp, mnt_list);
562 * Remove any installed vnode ops here so the individual VFSs don't
565 vfs_rm_vnodeops(&mp->mnt_vn_ops);
566 vfs_rm_vnodeops(&mp->mnt_vn_spec_ops);
567 vfs_rm_vnodeops(&mp->mnt_vn_fifo_ops);
569 if ((coveredvp = mp->mnt_vnodecovered) != NULLVP) {
570 coveredvp->v_mountedhere = NULL;
572 cache_drop(mp->mnt_ncp);
575 mp->mnt_vfc->vfc_refcount--;
576 if (!TAILQ_EMPTY(&mp->mnt_nvnodelist))
577 panic("unmount: dangling vnode");
578 lockmgr(&mp->mnt_lock, LK_RELEASE | LK_INTERLOCK, &ilock, td);
579 if (mp->mnt_kern_flag & MNTK_MWAIT)
586 * Sync each mounted filesystem.
590 static int syncprt = 0;
591 SYSCTL_INT(_debug, OID_AUTO, syncprt, CTLFLAG_RW, &syncprt, 0, "");
596 sync(struct sync_args *uap)
598 struct thread *td = curthread;
599 struct mount *mp, *nmp;
603 lwkt_gettoken(&ilock, &mountlist_token);
604 for (mp = TAILQ_FIRST(&mountlist); mp != NULL; mp = nmp) {
605 if (vfs_busy(mp, LK_NOWAIT, &ilock, td)) {
606 nmp = TAILQ_NEXT(mp, mnt_list);
609 if ((mp->mnt_flag & MNT_RDONLY) == 0) {
610 asyncflag = mp->mnt_flag & MNT_ASYNC;
611 mp->mnt_flag &= ~MNT_ASYNC;
612 vfs_msync(mp, MNT_NOWAIT);
613 VFS_SYNC(mp, MNT_NOWAIT, td);
614 mp->mnt_flag |= asyncflag;
616 lwkt_gettokref(&ilock);
617 nmp = TAILQ_NEXT(mp, mnt_list);
620 lwkt_reltoken(&ilock);
622 * print out buffer pool stat information on each sync() call.
631 /* XXX PRISON: could be per prison flag */
632 static int prison_quotas;
634 SYSCTL_INT(_kern_prison, OID_AUTO, quotas, CTLFLAG_RW, &prison_quotas, 0, "");
638 * quotactl_args(char *path, int fcmd, int uid, caddr_t arg)
640 * Change filesystem quotas.
644 quotactl(struct quotactl_args *uap)
646 struct nlookupdata nd;
654 if (p->p_ucred->cr_prison && !prison_quotas)
657 error = nlookup_init(&nd, SCARG(uap, path), UIO_USERSPACE, NLC_FOLLOW);
659 error = nlookup(&nd);
661 mp = nd.nl_ncp->nc_mount;
662 error = VFS_QUOTACTL(mp, SCARG(uap, cmd), SCARG(uap, uid),
663 SCARG(uap, arg), nd.nl_td);
670 kern_statfs(struct nlookupdata *nd, struct statfs *buf)
672 struct thread *td = curthread;
677 if ((error = nlookup(nd)) != 0)
679 mp = nd->nl_ncp->nc_mount;
681 if ((error = VFS_STATFS(mp, sp, td)) != 0)
683 sp->f_flags = mp->mnt_flag & MNT_VISFLAGMASK;
684 bcopy(sp, buf, sizeof(*buf));
685 /* Only root should have access to the fsid's. */
687 buf->f_fsid.val[0] = buf->f_fsid.val[1] = 0;
692 * statfs_args(char *path, struct statfs *buf)
694 * Get filesystem statistics.
697 statfs(struct statfs_args *uap)
699 struct nlookupdata nd;
703 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
705 error = kern_statfs(&nd, &buf);
708 error = copyout(&buf, uap->buf, sizeof(*uap->buf));
713 kern_fstatfs(int fd, struct statfs *buf)
715 struct thread *td = curthread;
716 struct proc *p = td->td_proc;
723 error = getvnode(p->p_fd, fd, &fp);
726 mp = ((struct vnode *)fp->f_data)->v_mount;
730 error = VFS_STATFS(mp, sp, td);
733 sp->f_flags = mp->mnt_flag & MNT_VISFLAGMASK;
734 bcopy(sp, buf, sizeof(*buf));
735 /* Only root should have access to the fsid's. */
737 buf->f_fsid.val[0] = buf->f_fsid.val[1] = 0;
742 * fstatfs_args(int fd, struct statfs *buf)
744 * Get filesystem statistics.
747 fstatfs(struct fstatfs_args *uap)
752 error = kern_fstatfs(uap->fd, &buf);
755 error = copyout(&buf, uap->buf, sizeof(*uap->buf));
760 * getfsstat_args(struct statfs *buf, long bufsize, int flags)
762 * Get statistics on all filesystems.
766 getfsstat(struct getfsstat_args *uap)
768 struct thread *td = curthread;
769 struct mount *mp, *nmp;
773 long count, maxcount, error;
775 maxcount = SCARG(uap, bufsize) / sizeof(struct statfs);
776 sfsp = (caddr_t)SCARG(uap, buf);
778 lwkt_gettoken(&ilock, &mountlist_token);
779 for (mp = TAILQ_FIRST(&mountlist); mp != NULL; mp = nmp) {
780 if (vfs_busy(mp, LK_NOWAIT, &ilock, td)) {
781 nmp = TAILQ_NEXT(mp, mnt_list);
784 if (sfsp && count < maxcount) {
787 * If MNT_NOWAIT or MNT_LAZY is specified, do not
788 * refresh the fsstat cache. MNT_NOWAIT or MNT_LAZY
789 * overrides MNT_WAIT.
791 if (((SCARG(uap, flags) & (MNT_LAZY|MNT_NOWAIT)) == 0 ||
792 (SCARG(uap, flags) & MNT_WAIT)) &&
793 (error = VFS_STATFS(mp, sp, td))) {
794 lwkt_gettokref(&ilock);
795 nmp = TAILQ_NEXT(mp, mnt_list);
799 sp->f_flags = mp->mnt_flag & MNT_VISFLAGMASK;
800 error = copyout(sp, sfsp, sizeof(*sp));
808 lwkt_gettokref(&ilock);
809 nmp = TAILQ_NEXT(mp, mnt_list);
812 lwkt_reltoken(&ilock);
813 if (sfsp && count > maxcount)
814 uap->sysmsg_result = maxcount;
816 uap->sysmsg_result = count;
821 * fchdir_args(int fd)
823 * Change current working directory to a given file descriptor.
827 fchdir(struct fchdir_args *uap)
829 struct thread *td = curthread;
830 struct proc *p = td->td_proc;
831 struct filedesc *fdp = p->p_fd;
832 struct vnode *vp, *ovp;
835 struct namecache *ncp, *oncp;
836 struct namecache *nct;
839 if ((error = getvnode(fdp, SCARG(uap, fd), &fp)) != 0)
841 vp = (struct vnode *)fp->f_data;
843 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY, td);
844 if (vp->v_type != VDIR || fp->f_ncp == NULL)
847 error = VOP_ACCESS(vp, VEXEC, p->p_ucred, td);
852 ncp = cache_hold(fp->f_ncp);
853 while (!error && (mp = vp->v_mountedhere) != NULL) {
854 error = nlookup_mp(mp, &nct);
856 cache_unlock(nct); /* leave ref intact */
859 error = vget(vp, LK_SHARED, td);
860 KKASSERT(error == 0);
867 oncp = fdp->fd_ncdir;
868 VOP_UNLOCK(vp, 0, td); /* leave ref intact */
881 kern_chdir(struct nlookupdata *nd)
883 struct thread *td = curthread;
884 struct proc *p = td->td_proc;
885 struct filedesc *fdp = p->p_fd;
886 struct vnode *vp, *ovp;
887 struct namecache *oncp;
890 if ((error = nlookup(nd)) != 0)
892 if ((vp = nd->nl_ncp->nc_vp) == NULL)
894 if ((error = vget(vp, LK_SHARED, td)) != 0)
897 error = checkvp_chdir(vp, td);
898 VOP_UNLOCK(vp, 0, td);
901 oncp = fdp->fd_ncdir;
902 cache_unlock(nd->nl_ncp); /* leave reference intact */
903 fdp->fd_ncdir = nd->nl_ncp;
915 * chdir_args(char *path)
917 * Change current working directory (``.'').
920 chdir(struct chdir_args *uap)
922 struct nlookupdata nd;
925 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
927 error = kern_chdir(&nd);
933 * Helper function for raised chroot(2) security function: Refuse if
934 * any filedescriptors are open directories.
937 chroot_refuse_vdir_fds(fdp)
938 struct filedesc *fdp;
945 for (fd = 0; fd < fdp->fd_nfiles ; fd++) {
946 error = getvnode(fdp, fd, &fp);
949 vp = (struct vnode *)fp->f_data;
950 if (vp->v_type != VDIR)
958 * This sysctl determines if we will allow a process to chroot(2) if it
959 * has a directory open:
960 * 0: disallowed for all processes.
961 * 1: allowed for processes that were not already chroot(2)'ed.
962 * 2: allowed for all processes.
965 static int chroot_allow_open_directories = 1;
967 SYSCTL_INT(_kern, OID_AUTO, chroot_allow_open_directories, CTLFLAG_RW,
968 &chroot_allow_open_directories, 0, "");
971 * chroot to the specified namecache entry. We obtain the vp from the
972 * namecache data. The passed ncp must be locked and referenced and will
973 * remain locked and referenced on return.
976 kern_chroot(struct nlookupdata *nd)
978 struct thread *td = curthread;
979 struct proc *p = td->td_proc;
980 struct filedesc *fdp = p->p_fd;
981 struct namecache *ncp;
986 * Only root can chroot
988 if ((error = suser_cred(p->p_ucred, PRISON_ROOT)) != 0)
991 if ((error = nlookup(nd)) != 0)
996 * Disallow open directory descriptors (fchdir() breakouts).
998 if (chroot_allow_open_directories == 0 ||
999 (chroot_allow_open_directories == 1 && fdp->fd_rdir != rootvnode)) {
1000 if ((error = chroot_refuse_vdir_fds(fdp)) != 0)
1003 if ((vp = ncp->nc_vp) == NULL)
1006 if ((error = vget(vp, LK_SHARED, td)) != 0)
1010 * Check the validity of vp as a directory to change to and
1011 * associate it with rdir/jdir.
1013 error = checkvp_chdir(vp, td);
1014 VOP_UNLOCK(vp, 0, td); /* leave reference intact */
1016 vrele(fdp->fd_rdir);
1017 fdp->fd_rdir = vp; /* reference inherited by fd_rdir */
1018 cache_drop(fdp->fd_nrdir);
1019 fdp->fd_nrdir = cache_hold(ncp);
1020 if (fdp->fd_jdir == NULL) {
1023 fdp->fd_njdir = cache_hold(ncp);
1032 * chroot_args(char *path)
1034 * Change notion of root (``/'') directory.
1038 chroot(struct chroot_args *uap)
1040 struct thread *td = curthread;
1041 struct nlookupdata nd;
1044 KKASSERT(td->td_proc);
1045 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
1047 error = kern_chroot(&nd);
1053 * Common routine for chroot and chdir. Given a locked, referenced vnode,
1054 * determine whether it is legal to chdir to the vnode. The vnode's state
1055 * is not changed by this call.
1058 checkvp_chdir(struct vnode *vp, struct thread *td)
1062 if (vp->v_type != VDIR)
1065 error = VOP_ACCESS(vp, VEXEC, td->td_proc->p_ucred, td);
1070 kern_open(struct nlookupdata *nd, int oflags, int mode, int *res)
1072 struct thread *td = curthread;
1073 struct proc *p = td->td_proc;
1074 struct filedesc *fdp = p->p_fd;
1079 int type, indx, error;
1082 if ((oflags & O_ACCMODE) == O_ACCMODE)
1084 flags = FFLAGS(oflags);
1085 error = falloc(p, &nfp, NULL);
1089 cmode = ((mode &~ fdp->fd_cmask) & ALLPERMS) &~ S_ISTXT;
1092 * XXX p_dupfd is a real mess. It allows a device to return a
1093 * file descriptor to be duplicated rather then doing the open
1099 * Call vn_open() to do the lookup and assign the vnode to the
1100 * file pointer. vn_open() does not change the ref count on fp
1101 * and the vnode, on success, will be inherited by the file pointer
1104 nd->nl_flags |= NLC_LOCKVP;
1105 error = vn_open(nd, fp, flags, cmode);
1109 * handle special fdopen() case. bleh. dupfdopen() is
1110 * responsible for dropping the old contents of ofiles[indx]
1113 * Note that if fsetfd() succeeds it will add a ref to fp
1114 * which represents the fd_ofiles[] assignment. We must still
1115 * drop our reference.
1117 if ((error == ENODEV || error == ENXIO) && p->p_dupfd >= 0) {
1118 if (fsetfd(p, fp, &indx) == 0) {
1119 error = dupfdopen(fdp, indx, p->p_dupfd, flags, error);
1122 fdrop(fp, td); /* our ref */
1125 if (fdp->fd_ofiles[indx] == fp) {
1126 fdp->fd_ofiles[indx] = NULL;
1127 fdrop(fp, td); /* fd_ofiles[] ref */
1131 fdrop(fp, td); /* our ref */
1132 if (error == ERESTART)
1138 * ref the vnode for ourselves so it can't be ripped out from under
1139 * is. XXX need an ND flag to request that the vnode be returned
1142 vp = (struct vnode *)fp->f_data;
1144 if ((error = fsetfd(p, fp, &indx)) != 0) {
1151 * If no error occurs the vp will have been assigned to the file
1157 * There should be 2 references on the file, one from the descriptor
1158 * table, and one for us.
1160 * Handle the case where someone closed the file (via its file
1161 * descriptor) while we were blocked. The end result should look
1162 * like opening the file succeeded but it was immediately closed.
1164 if (fp->f_count == 1) {
1165 KASSERT(fdp->fd_ofiles[indx] != fp,
1166 ("Open file descriptor lost all refs"));
1174 if (flags & (O_EXLOCK | O_SHLOCK)) {
1175 lf.l_whence = SEEK_SET;
1178 if (flags & O_EXLOCK)
1179 lf.l_type = F_WRLCK;
1181 lf.l_type = F_RDLCK;
1183 if ((flags & FNONBLOCK) == 0)
1186 if ((error = VOP_ADVLOCK(vp, (caddr_t)fp, F_SETLK, &lf, type)) != 0) {
1188 * lock request failed. Normally close the descriptor
1189 * but handle the case where someone might have dup()d
1190 * it when we weren't looking. One reference is
1191 * owned by the descriptor array, the other by us.
1194 if (fdp->fd_ofiles[indx] == fp) {
1195 fdp->fd_ofiles[indx] = NULL;
1201 fp->f_flag |= FHASLOCK;
1203 /* assert that vn_open created a backing object if one is needed */
1204 KASSERT(!vn_canvmio(vp) || VOP_GETVOBJECT(vp, NULL) == 0,
1205 ("open: vmio vnode has no backing object after vn_open"));
1210 * release our private reference, leaving the one associated with the
1211 * descriptor table intact.
1219 * open_args(char *path, int flags, int mode)
1221 * Check permissions, allocate an open file structure,
1222 * and call the device open routine if any.
1225 open(struct open_args *uap)
1227 struct nlookupdata nd;
1230 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
1232 error = kern_open(&nd, uap->flags,
1233 uap->mode, &uap->sysmsg_result);
1240 kern_mknod(struct nlookupdata *nd, int mode, int dev)
1242 struct namecache *ncp;
1243 struct thread *td = curthread;
1244 struct proc *p = td->td_proc;
1252 switch (mode & S_IFMT) {
1258 error = suser_cred(p->p_ucred, PRISON_ROOT);
1265 nd->nl_flags |= NLC_CREATE;
1266 if ((error = nlookup(nd)) != 0)
1273 vattr.va_mode = (mode & ALLPERMS) &~ p->p_fd->fd_cmask;
1274 vattr.va_rdev = dev;
1277 switch (mode & S_IFMT) {
1278 case S_IFMT: /* used by badsect to flag bad sectors */
1279 vattr.va_type = VBAD;
1282 vattr.va_type = VCHR;
1285 vattr.va_type = VBLK;
1296 error = VOP_NWHITEOUT(ncp, nd->nl_cred, NAMEI_CREATE);
1299 error = VOP_NMKNOD(ncp, &vp, nd->nl_cred, &vattr);
1308 * mknod_args(char *path, int mode, int dev)
1310 * Create a special file.
1313 mknod(struct mknod_args *uap)
1315 struct nlookupdata nd;
1318 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, 0);
1320 error = kern_mknod(&nd, uap->mode, uap->dev);
1326 kern_mkfifo(struct nlookupdata *nd, int mode)
1328 struct namecache *ncp;
1329 struct thread *td = curthread;
1330 struct proc *p = td->td_proc;
1337 nd->nl_flags |= NLC_CREATE;
1338 if ((error = nlookup(nd)) != 0)
1345 vattr.va_type = VFIFO;
1346 vattr.va_mode = (mode & ALLPERMS) &~ p->p_fd->fd_cmask;
1348 error = VOP_NMKNOD(ncp, &vp, nd->nl_cred, &vattr);
1355 * mkfifo_args(char *path, int mode)
1357 * Create a named pipe.
1360 mkfifo(struct mkfifo_args *uap)
1362 struct nlookupdata nd;
1365 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, 0);
1367 error = kern_mkfifo(&nd, uap->mode);
1373 kern_link(struct nlookupdata *nd, struct nlookupdata *linknd)
1375 struct thread *td = curthread;
1380 * Lookup the source and obtained a locked vnode.
1382 * XXX relookup on vget failure / race ?
1385 if ((error = nlookup(nd)) != 0)
1387 vp = nd->nl_ncp->nc_vp;
1388 KKASSERT(vp != NULL);
1389 if (vp->v_type == VDIR)
1390 return (EPERM); /* POSIX */
1391 if ((error = vget(vp, LK_EXCLUSIVE, td)) != 0)
1395 * Unlock the source so we can lookup the target without deadlocking
1396 * (XXX vp is locked already, possible other deadlock?). The target
1399 KKASSERT(nd->nl_flags & NLC_NCPISLOCKED);
1400 nd->nl_flags &= ~NLC_NCPISLOCKED;
1401 cache_unlock(nd->nl_ncp);
1403 linknd->nl_flags |= NLC_CREATE;
1404 if ((error = nlookup(linknd)) != 0) {
1408 if (linknd->nl_ncp->nc_vp) {
1414 * Finally run the new API VOP.
1416 error = VOP_NLINK(linknd->nl_ncp, vp, linknd->nl_cred);
1422 * link_args(char *path, char *link)
1424 * Make a hard file link.
1427 link(struct link_args *uap)
1429 struct nlookupdata nd, linknd;
1432 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
1434 error = nlookup_init(&linknd, uap->link, UIO_USERSPACE, 0);
1436 error = kern_link(&nd, &linknd);
1437 nlookup_done(&linknd);
1444 kern_symlink(struct nlookupdata *nd, char *path, int mode)
1446 struct namecache *ncp;
1452 nd->nl_flags |= NLC_CREATE;
1453 if ((error = nlookup(nd)) != 0)
1460 vattr.va_mode = mode;
1461 error = VOP_NSYMLINK(ncp, &vp, nd->nl_cred, &vattr, path);
1468 * symlink(char *path, char *link)
1470 * Make a symbolic link.
1473 symlink(struct symlink_args *uap)
1475 struct thread *td = curthread;
1476 struct nlookupdata nd;
1481 path = zalloc(namei_zone);
1482 error = copyinstr(uap->path, path, MAXPATHLEN, NULL);
1484 error = nlookup_init(&nd, uap->link, UIO_USERSPACE, 0);
1486 mode = ACCESSPERMS & ~td->td_proc->p_fd->fd_cmask;
1487 error = kern_symlink(&nd, path, mode);
1491 zfree(namei_zone, path);
1496 * undelete_args(char *path)
1498 * Delete a whiteout from the filesystem.
1502 undelete(struct undelete_args *uap)
1504 struct nlookupdata nd;
1507 error = nlookup_init(&nd, SCARG(uap, path), UIO_USERSPACE, 0);
1509 nd.nl_flags |= NLC_DELETE;
1511 error = nlookup(&nd);
1513 error = VOP_NWHITEOUT(nd.nl_ncp, nd.nl_cred, NAMEI_DELETE);
1519 kern_unlink(struct nlookupdata *nd)
1521 struct namecache *ncp;
1525 nd->nl_flags |= NLC_DELETE;
1526 if ((error = nlookup(nd)) != 0)
1529 error = VOP_NREMOVE(ncp, nd->nl_cred);
1534 * unlink_args(char *path)
1536 * Delete a name from the filesystem.
1539 unlink(struct unlink_args *uap)
1541 struct nlookupdata nd;
1544 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, 0);
1546 error = kern_unlink(&nd);
1552 kern_lseek(int fd, off_t offset, int whence, off_t *res)
1554 struct thread *td = curthread;
1555 struct proc *p = td->td_proc;
1556 struct filedesc *fdp = p->p_fd;
1561 if (fd >= fdp->fd_nfiles ||
1562 (fp = fdp->fd_ofiles[fd]) == NULL)
1564 if (fp->f_type != DTYPE_VNODE)
1568 fp->f_offset += offset;
1571 error=VOP_GETATTR((struct vnode *)fp->f_data, &vattr, td);
1574 fp->f_offset = offset + vattr.va_size;
1577 fp->f_offset = offset;
1582 *res = fp->f_offset;
1587 * lseek_args(int fd, int pad, off_t offset, int whence)
1589 * Reposition read/write file offset.
1592 lseek(struct lseek_args *uap)
1596 error = kern_lseek(uap->fd, uap->offset, uap->whence,
1597 &uap->sysmsg_offset);
1603 kern_access(struct nlookupdata *nd, int aflags)
1605 struct thread *td = curthread;
1609 if ((error = nlookup(nd)) != 0)
1611 error = cache_vget(nd->nl_ncp, nd->nl_cred, LK_EXCLUSIVE, &vp);
1615 /* Flags == 0 means only check for existence. */
1624 if ((flags & VWRITE) == 0 || (error = vn_writechk(vp)) == 0)
1625 error = VOP_ACCESS(vp, flags, nd->nl_cred, td);
1632 * access_args(char *path, int flags)
1634 * Check access permissions.
1637 access(struct access_args *uap)
1639 struct nlookupdata nd;
1642 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
1644 error = kern_access(&nd, uap->flags);
1650 kern_stat(struct nlookupdata *nd, struct stat *st)
1656 if ((error = nlookup(nd)) != 0)
1658 if ((vp = nd->nl_ncp->nc_vp) == NULL)
1662 if ((error = vget(vp, LK_SHARED, td)) != 0)
1664 error = vn_stat(vp, st, td);
1670 * stat_args(char *path, struct stat *ub)
1672 * Get file status; this version follows links.
1675 stat(struct stat_args *uap)
1677 struct nlookupdata nd;
1681 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
1683 error = kern_stat(&nd, &st);
1685 error = copyout(&st, uap->ub, sizeof(*uap->ub));
1692 * lstat_args(char *path, struct stat *ub)
1694 * Get file status; this version does not follow links.
1697 lstat(struct lstat_args *uap)
1699 struct nlookupdata nd;
1703 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, 0);
1705 error = kern_stat(&nd, &st);
1707 error = copyout(&st, uap->ub, sizeof(*uap->ub));
1718 nsb->st_dev = sb->st_dev;
1719 nsb->st_ino = sb->st_ino;
1720 nsb->st_mode = sb->st_mode;
1721 nsb->st_nlink = sb->st_nlink;
1722 nsb->st_uid = sb->st_uid;
1723 nsb->st_gid = sb->st_gid;
1724 nsb->st_rdev = sb->st_rdev;
1725 nsb->st_atimespec = sb->st_atimespec;
1726 nsb->st_mtimespec = sb->st_mtimespec;
1727 nsb->st_ctimespec = sb->st_ctimespec;
1728 nsb->st_size = sb->st_size;
1729 nsb->st_blocks = sb->st_blocks;
1730 nsb->st_blksize = sb->st_blksize;
1731 nsb->st_flags = sb->st_flags;
1732 nsb->st_gen = sb->st_gen;
1733 nsb->st_qspare[0] = sb->st_qspare[0];
1734 nsb->st_qspare[1] = sb->st_qspare[1];
1738 * nstat_args(char *path, struct nstat *ub)
1742 nstat(struct nstat_args *uap)
1744 struct thread *td = curthread;
1748 struct nlookupdata nd;
1752 error = nlookup_init(&nd, SCARG(uap, path), UIO_USERSPACE, NLC_FOLLOW);
1754 error = nlookup(&nd);
1756 error = cache_vget(nd.nl_ncp, nd.nl_cred, LK_EXCLUSIVE, &vp);
1759 error = vn_stat(vp, &sb, td);
1762 cvtnstat(&sb, &nsb);
1763 error = copyout(&nsb, SCARG(uap, ub), sizeof(nsb));
1770 * lstat_args(char *path, struct stat *ub)
1772 * Get file status; this version does not follow links.
1776 nlstat(struct nlstat_args *uap)
1778 struct thread *td = curthread;
1782 struct nlookupdata nd;
1786 error = nlookup_init(&nd, SCARG(uap, path), UIO_USERSPACE, 0);
1788 error = nlookup(&nd);
1790 error = cache_vget(nd.nl_ncp, nd.nl_cred, LK_EXCLUSIVE, &vp);
1793 error = vn_stat(vp, &sb, td);
1796 cvtnstat(&sb, &nsb);
1797 error = copyout(&nsb, SCARG(uap, ub), sizeof(nsb));
1804 * pathconf_Args(char *path, int name)
1806 * Get configurable pathname variables.
1810 pathconf(struct pathconf_args *uap)
1812 struct nlookupdata nd;
1817 error = nlookup_init(&nd, SCARG(uap, path), UIO_USERSPACE, NLC_FOLLOW);
1819 error = nlookup(&nd);
1821 error = cache_vget(nd.nl_ncp, nd.nl_cred, LK_EXCLUSIVE, &vp);
1824 error = VOP_PATHCONF(vp, SCARG(uap, name), uap->sysmsg_fds);
1832 * kern_readlink isn't properly split yet. There is a copyin burried
1833 * in VOP_READLINK().
1836 kern_readlink(struct nlookupdata *nd, char *buf, int count, int *res)
1838 struct thread *td = curthread;
1839 struct proc *p = td->td_proc;
1845 if ((error = nlookup(nd)) != 0)
1847 error = cache_vget(nd->nl_ncp, nd->nl_cred, LK_EXCLUSIVE, &vp);
1850 if (vp->v_type != VLNK) {
1853 aiov.iov_base = buf;
1854 aiov.iov_len = count;
1855 auio.uio_iov = &aiov;
1856 auio.uio_iovcnt = 1;
1857 auio.uio_offset = 0;
1858 auio.uio_rw = UIO_READ;
1859 auio.uio_segflg = UIO_USERSPACE;
1861 auio.uio_resid = count;
1862 error = VOP_READLINK(vp, &auio, p->p_ucred);
1865 *res = count - auio.uio_resid;
1870 * readlink_args(char *path, char *buf, int count)
1872 * Return target name of a symbolic link.
1875 readlink(struct readlink_args *uap)
1877 struct nlookupdata nd;
1880 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, 0);
1882 error = kern_readlink(&nd, uap->buf, uap->count,
1883 &uap->sysmsg_result);
1890 setfflags(struct vnode *vp, int flags)
1892 struct thread *td = curthread;
1893 struct proc *p = td->td_proc;
1898 * Prevent non-root users from setting flags on devices. When
1899 * a device is reused, users can retain ownership of the device
1900 * if they are allowed to set flags and programs assume that
1901 * chown can't fail when done as root.
1903 if ((vp->v_type == VCHR || vp->v_type == VBLK) &&
1904 ((error = suser_cred(p->p_ucred, PRISON_ROOT)) != 0))
1907 VOP_LEASE(vp, td, p->p_ucred, LEASE_WRITE);
1908 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY, td);
1910 vattr.va_flags = flags;
1911 error = VOP_SETATTR(vp, &vattr, p->p_ucred, td);
1912 VOP_UNLOCK(vp, 0, td);
1917 * chflags(char *path, int flags)
1919 * Change flags of a file given a path name.
1923 chflags(struct chflags_args *uap)
1925 struct nlookupdata nd;
1930 error = nlookup_init(&nd, SCARG(uap, path), UIO_USERSPACE, NLC_FOLLOW);
1931 /* XXX Add NLC flag indicating modifying operation? */
1933 error = nlookup(&nd);
1935 error = cache_vref(nd.nl_ncp, nd.nl_cred, &vp);
1938 error = setfflags(vp, SCARG(uap, flags));
1945 * fchflags_args(int fd, int flags)
1947 * Change flags of a file given a file descriptor.
1951 fchflags(struct fchflags_args *uap)
1953 struct thread *td = curthread;
1954 struct proc *p = td->td_proc;
1958 if ((error = getvnode(p->p_fd, SCARG(uap, fd), &fp)) != 0)
1960 return setfflags((struct vnode *) fp->f_data, SCARG(uap, flags));
1964 setfmode(struct vnode *vp, int mode)
1966 struct thread *td = curthread;
1967 struct proc *p = td->td_proc;
1971 VOP_LEASE(vp, td, p->p_ucred, LEASE_WRITE);
1972 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY, td);
1974 vattr.va_mode = mode & ALLPERMS;
1975 error = VOP_SETATTR(vp, &vattr, p->p_ucred, td);
1976 VOP_UNLOCK(vp, 0, td);
1981 kern_chmod(struct nlookupdata *nd, int mode)
1986 /* XXX Add NLC flag indicating modifying operation? */
1987 if ((error = nlookup(nd)) != 0)
1989 if ((error = cache_vref(nd->nl_ncp, nd->nl_cred, &vp)) != 0)
1991 error = setfmode(vp, mode);
1997 * chmod_args(char *path, int mode)
1999 * Change mode of a file given path name.
2003 chmod(struct chmod_args *uap)
2005 struct nlookupdata nd;
2008 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
2010 error = kern_chmod(&nd, uap->mode);
2016 * lchmod_args(char *path, int mode)
2018 * Change mode of a file given path name (don't follow links.)
2022 lchmod(struct lchmod_args *uap)
2024 struct nlookupdata nd;
2027 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, 0);
2029 error = kern_chmod(&nd, uap->mode);
2035 * fchmod_args(int fd, int mode)
2037 * Change mode of a file given a file descriptor.
2041 fchmod(struct fchmod_args *uap)
2043 struct thread *td = curthread;
2044 struct proc *p = td->td_proc;
2048 if ((error = getvnode(p->p_fd, SCARG(uap, fd), &fp)) != 0)
2050 return setfmode((struct vnode *)fp->f_data, SCARG(uap, mode));
2054 setfown(struct vnode *vp, uid_t uid, gid_t gid)
2056 struct thread *td = curthread;
2057 struct proc *p = td->td_proc;
2061 VOP_LEASE(vp, td, p->p_ucred, LEASE_WRITE);
2062 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY, td);
2066 error = VOP_SETATTR(vp, &vattr, p->p_ucred, td);
2067 VOP_UNLOCK(vp, 0, td);
2072 kern_chown(struct nlookupdata *nd, int uid, int gid)
2077 /* XXX Add NLC flag indicating modifying operation? */
2078 if ((error = nlookup(nd)) != 0)
2080 if ((error = cache_vref(nd->nl_ncp, nd->nl_cred, &vp)) != 0)
2082 error = setfown(vp, uid, gid);
2088 * chown(char *path, int uid, int gid)
2090 * Set ownership given a path name.
2093 chown(struct chown_args *uap)
2095 struct nlookupdata nd;
2098 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
2100 error = kern_chown(&nd, uap->uid, uap->gid);
2106 * lchown_args(char *path, int uid, int gid)
2108 * Set ownership given a path name, do not cross symlinks.
2111 lchown(struct lchown_args *uap)
2113 struct nlookupdata nd;
2116 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, 0);
2118 error = kern_chown(&nd, uap->uid, uap->gid);
2124 * fchown_args(int fd, int uid, int gid)
2126 * Set ownership given a file descriptor.
2130 fchown(struct fchown_args *uap)
2132 struct thread *td = curthread;
2133 struct proc *p = td->td_proc;
2137 if ((error = getvnode(p->p_fd, SCARG(uap, fd), &fp)) != 0)
2139 return setfown((struct vnode *)fp->f_data,
2140 SCARG(uap, uid), SCARG(uap, gid));
2144 getutimes(const struct timeval *tvp, struct timespec *tsp)
2146 struct timeval tv[2];
2150 TIMEVAL_TO_TIMESPEC(&tv[0], &tsp[0]);
2153 TIMEVAL_TO_TIMESPEC(&tvp[0], &tsp[0]);
2154 TIMEVAL_TO_TIMESPEC(&tvp[1], &tsp[1]);
2160 setutimes(struct vnode *vp, const struct timespec *ts, int nullflag)
2162 struct thread *td = curthread;
2163 struct proc *p = td->td_proc;
2167 VOP_LEASE(vp, td, p->p_ucred, LEASE_WRITE);
2168 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY, td);
2170 vattr.va_atime = ts[0];
2171 vattr.va_mtime = ts[1];
2173 vattr.va_vaflags |= VA_UTIMES_NULL;
2174 error = VOP_SETATTR(vp, &vattr, p->p_ucred, td);
2175 VOP_UNLOCK(vp, 0, td);
2180 kern_utimes(struct nlookupdata *nd, struct timeval *tptr)
2182 struct timespec ts[2];
2186 if ((error = getutimes(tptr, ts)) != 0)
2188 /* XXX Add NLC flag indicating modifying operation? */
2189 if ((error = nlookup(nd)) != 0)
2191 if ((error = cache_vref(nd->nl_ncp, nd->nl_cred, &vp)) != 0)
2193 error = setutimes(vp, ts, tptr == NULL);
2199 * utimes_args(char *path, struct timeval *tptr)
2201 * Set the access and modification times of a file.
2204 utimes(struct utimes_args *uap)
2206 struct timeval tv[2];
2207 struct nlookupdata nd;
2211 error = copyin(uap->tptr, tv, sizeof(tv));
2215 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
2217 error = kern_utimes(&nd, uap->tptr ? tv : NULL);
2223 * lutimes_args(char *path, struct timeval *tptr)
2225 * Set the access and modification times of a file.
2228 lutimes(struct lutimes_args *uap)
2230 struct timeval tv[2];
2231 struct nlookupdata nd;
2235 error = copyin(uap->tptr, tv, sizeof(tv));
2239 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, 0);
2241 error = kern_utimes(&nd, uap->tptr ? tv : NULL);
2247 kern_futimes(int fd, struct timeval *tptr)
2249 struct thread *td = curthread;
2250 struct proc *p = td->td_proc;
2251 struct timespec ts[2];
2255 error = getutimes(tptr, ts);
2258 error = getvnode(p->p_fd, fd, &fp);
2261 error = setutimes((struct vnode *)fp->f_data, ts, tptr == NULL);
2266 * futimes_args(int fd, struct timeval *tptr)
2268 * Set the access and modification times of a file.
2271 futimes(struct futimes_args *uap)
2273 struct timeval tv[2];
2277 error = copyin(uap->tptr, tv, sizeof(tv));
2282 error = kern_futimes(uap->fd, uap->tptr ? tv : NULL);
2288 kern_truncate(struct nlookupdata *nd, off_t length)
2296 /* XXX Add NLC flag indicating modifying operation? */
2297 if ((error = nlookup(nd)) != 0)
2299 if ((error = cache_vref(nd->nl_ncp, nd->nl_cred, &vp)) != 0)
2301 VOP_LEASE(vp, nd->nl_td, nd->nl_cred, LEASE_WRITE);
2302 if ((error = vn_lock(vp, LK_EXCLUSIVE | LK_RETRY, nd->nl_td)) != 0) {
2306 if (vp->v_type == VDIR) {
2308 } else if ((error = vn_writechk(vp)) == 0 &&
2309 (error = VOP_ACCESS(vp, VWRITE, nd->nl_cred, nd->nl_td)) == 0) {
2311 vattr.va_size = length;
2312 error = VOP_SETATTR(vp, &vattr, nd->nl_cred, nd->nl_td);
2319 * truncate(char *path, int pad, off_t length)
2321 * Truncate a file given its path name.
2324 truncate(struct truncate_args *uap)
2326 struct nlookupdata nd;
2329 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
2331 error = kern_truncate(&nd, uap->length);
2337 kern_ftruncate(int fd, off_t length)
2339 struct thread *td = curthread;
2340 struct proc *p = td->td_proc;
2348 if ((error = getvnode(p->p_fd, fd, &fp)) != 0)
2350 if ((fp->f_flag & FWRITE) == 0)
2352 vp = (struct vnode *)fp->f_data;
2353 VOP_LEASE(vp, td, p->p_ucred, LEASE_WRITE);
2354 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY, td);
2355 if (vp->v_type == VDIR)
2357 else if ((error = vn_writechk(vp)) == 0) {
2359 vattr.va_size = length;
2360 error = VOP_SETATTR(vp, &vattr, fp->f_cred, td);
2362 VOP_UNLOCK(vp, 0, td);
2367 * ftruncate_args(int fd, int pad, off_t length)
2369 * Truncate a file given a file descriptor.
2372 ftruncate(struct ftruncate_args *uap)
2376 error = kern_ftruncate(uap->fd, uap->length);
2384 * Sync an open file.
2388 fsync(struct fsync_args *uap)
2390 struct thread *td = curthread;
2391 struct proc *p = td->td_proc;
2397 if ((error = getvnode(p->p_fd, SCARG(uap, fd), &fp)) != 0)
2399 vp = (struct vnode *)fp->f_data;
2400 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY, td);
2401 if (VOP_GETVOBJECT(vp, &obj) == 0)
2402 vm_object_page_clean(obj, 0, 0, 0);
2403 if ((error = VOP_FSYNC(vp, MNT_WAIT, td)) == 0 &&
2404 vp->v_mount && (vp->v_mount->mnt_flag & MNT_SOFTDEP) &&
2406 error = (*bioops.io_fsync)(vp);
2407 VOP_UNLOCK(vp, 0, td);
2412 kern_rename(struct nlookupdata *fromnd, struct nlookupdata *tond)
2414 struct namecache *fncpd;
2415 struct namecache *tncpd;
2416 struct namecache *ncp;
2421 if ((error = nlookup(fromnd)) != 0)
2423 if ((fncpd = fromnd->nl_ncp->nc_parent) == NULL)
2428 * unlock the source ncp so we can lookup the target ncp without
2429 * deadlocking. The target may or may not exist so we do not check
2430 * for a target vp like kern_mkdir() and other creation functions do.
2432 * The source and target directories are ref'd and rechecked after
2433 * everything is relocked to determine if the source or target file
2436 KKASSERT(fromnd->nl_flags & NLC_NCPISLOCKED);
2437 fromnd->nl_flags &= ~NLC_NCPISLOCKED;
2438 cache_unlock(fromnd->nl_ncp);
2440 tond->nl_flags |= NLC_CREATE;
2441 if ((error = nlookup(tond)) != 0) {
2445 if ((tncpd = tond->nl_ncp->nc_parent) == NULL) {
2452 * If the source and target are the same there is nothing to do
2454 if (fromnd->nl_ncp == tond->nl_ncp) {
2461 * relock the source ncp
2463 if (cache_lock_nonblock(fromnd->nl_ncp) == 0) {
2464 cache_resolve(fromnd->nl_ncp, fromnd->nl_cred);
2465 } else if (fromnd->nl_ncp > tond->nl_ncp) {
2466 cache_lock(fromnd->nl_ncp);
2467 cache_resolve(fromnd->nl_ncp, fromnd->nl_cred);
2469 cache_unlock(tond->nl_ncp);
2470 cache_lock(fromnd->nl_ncp);
2471 cache_resolve(fromnd->nl_ncp, fromnd->nl_cred);
2472 cache_lock(tond->nl_ncp);
2473 cache_resolve(tond->nl_ncp, tond->nl_cred);
2475 fromnd->nl_flags |= NLC_NCPISLOCKED;
2478 * make sure the parent directories linkages are the same
2480 if (fncpd != fromnd->nl_ncp->nc_parent ||
2481 tncpd != tond->nl_ncp->nc_parent) {
2488 * Both the source and target must be within the same filesystem and
2489 * in the same filesystem as their parent directories within the
2490 * namecache topology.
2492 mp = fncpd->nc_mount;
2493 if (mp != tncpd->nc_mount || mp != fromnd->nl_ncp->nc_mount ||
2494 mp != tond->nl_ncp->nc_mount) {
2501 * If the target exists and either the source or target is a directory,
2502 * then both must be directories.
2504 if (tond->nl_ncp->nc_vp) {
2505 if (fromnd->nl_ncp->nc_vp->v_type == VDIR) {
2506 if (tond->nl_ncp->nc_vp->v_type != VDIR)
2508 } else if (tond->nl_ncp->nc_vp->v_type == VDIR) {
2514 * You cannot rename a source into itself or a subdirectory of itself.
2515 * We check this by travsersing the target directory upwards looking
2516 * for a match against the source.
2519 for (ncp = tncpd; ncp; ncp = ncp->nc_parent) {
2520 if (fromnd->nl_ncp == ncp) {
2531 error = VOP_NRENAME(fromnd->nl_ncp, tond->nl_ncp, tond->nl_cred);
2536 * rename_args(char *from, char *to)
2538 * Rename files. Source and destination must either both be directories,
2539 * or both not be directories. If target is a directory, it must be empty.
2542 rename(struct rename_args *uap)
2544 struct nlookupdata fromnd, tond;
2547 error = nlookup_init(&fromnd, uap->from, UIO_USERSPACE, 0);
2549 error = nlookup_init(&tond, uap->to, UIO_USERSPACE, 0);
2551 error = kern_rename(&fromnd, &tond);
2552 nlookup_done(&tond);
2554 nlookup_done(&fromnd);
2559 kern_mkdir(struct nlookupdata *nd, int mode)
2561 struct thread *td = curthread;
2562 struct proc *p = td->td_proc;
2563 struct namecache *ncp;
2569 nd->nl_flags |= NLC_WILLBEDIR | NLC_CREATE;
2570 if ((error = nlookup(nd)) != 0)
2578 vattr.va_type = VDIR;
2579 vattr.va_mode = (mode & ACCESSPERMS) &~ p->p_fd->fd_cmask;
2582 error = VOP_NMKDIR(ncp, &vp, p->p_ucred, &vattr);
2589 * mkdir_args(char *path, int mode)
2591 * Make a directory file.
2595 mkdir(struct mkdir_args *uap)
2597 struct nlookupdata nd;
2600 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, 0);
2602 error = kern_mkdir(&nd, uap->mode);
2608 kern_rmdir(struct nlookupdata *nd)
2610 struct namecache *ncp;
2614 nd->nl_flags |= NLC_DELETE;
2615 if ((error = nlookup(nd)) != 0)
2619 error = VOP_NRMDIR(ncp, nd->nl_cred);
2624 * rmdir_args(char *path)
2626 * Remove a directory file.
2630 rmdir(struct rmdir_args *uap)
2632 struct nlookupdata nd;
2635 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, 0);
2637 error = kern_rmdir(&nd);
2643 kern_getdirentries(int fd, char *buf, u_int count, long *basep, int *res)
2645 struct thread *td = curthread;
2646 struct proc *p = td->td_proc;
2654 if ((error = getvnode(p->p_fd, fd, &fp)) != 0)
2656 if ((fp->f_flag & FREAD) == 0)
2658 vp = (struct vnode *)fp->f_data;
2660 if (vp->v_type != VDIR)
2662 aiov.iov_base = buf;
2663 aiov.iov_len = count;
2664 auio.uio_iov = &aiov;
2665 auio.uio_iovcnt = 1;
2666 auio.uio_rw = UIO_READ;
2667 auio.uio_segflg = UIO_USERSPACE;
2669 auio.uio_resid = count;
2670 /* vn_lock(vp, LK_SHARED | LK_RETRY, td); */
2671 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY, td);
2672 loff = auio.uio_offset = fp->f_offset;
2673 error = VOP_READDIR(vp, &auio, fp->f_cred, &eofflag, NULL, NULL);
2674 fp->f_offset = auio.uio_offset;
2675 VOP_UNLOCK(vp, 0, td);
2678 if (count == auio.uio_resid) {
2679 if (union_dircheckp) {
2680 error = union_dircheckp(td, &vp, fp);
2686 if ((vp->v_flag & VROOT) &&
2687 (vp->v_mount->mnt_flag & MNT_UNION)) {
2688 struct vnode *tvp = vp;
2689 vp = vp->v_mount->mnt_vnodecovered;
2691 fp->f_data = (caddr_t)vp;
2700 *res = count - auio.uio_resid;
2705 * getdirentries_args(int fd, char *buf, u_int conut, long *basep)
2707 * Read a block of directory entries in a file system independent format.
2710 getdirentries(struct getdirentries_args *uap)
2715 error = kern_getdirentries(uap->fd, uap->buf, uap->count, &base,
2716 &uap->sysmsg_result);
2719 error = copyout(&base, uap->basep, sizeof(*uap->basep));
2724 * getdents_args(int fd, char *buf, size_t count)
2727 getdents(struct getdents_args *uap)
2731 error = kern_getdirentries(uap->fd, uap->buf, uap->count, NULL,
2732 &uap->sysmsg_result);
2738 * umask(int newmask)
2740 * Set the mode mask for creation of filesystem nodes.
2745 umask(struct umask_args *uap)
2747 struct thread *td = curthread;
2748 struct proc *p = td->td_proc;
2749 struct filedesc *fdp;
2752 uap->sysmsg_result = fdp->fd_cmask;
2753 fdp->fd_cmask = SCARG(uap, newmask) & ALLPERMS;
2758 * revoke(char *path)
2760 * Void all references to file by ripping underlying filesystem
2765 revoke(struct revoke_args *uap)
2767 struct thread *td = curthread;
2768 struct nlookupdata nd;
2775 error = nlookup_init(&nd, SCARG(uap, path), UIO_USERSPACE, NLC_FOLLOW);
2777 error = nlookup(&nd);
2779 error = cache_vref(nd.nl_ncp, nd.nl_cred, &vp);
2780 cred = crhold(nd.nl_cred);
2783 if (vp->v_type != VCHR && vp->v_type != VBLK)
2786 error = VOP_GETATTR(vp, &vattr, td);
2787 if (error == 0 && cred->cr_uid != vattr.va_uid)
2788 error = suser_cred(cred, PRISON_ROOT);
2789 if (error == 0 && count_udev(vp->v_udev) > 0) {
2790 if ((error = vx_lock(vp)) == 0) {
2791 VOP_REVOKE(vp, REVOKEALL);
2802 * Convert a user file descriptor to a kernel file entry.
2805 getvnode(struct filedesc *fdp, int fd, struct file **fpp)
2809 if ((u_int)fd >= fdp->fd_nfiles ||
2810 (fp = fdp->fd_ofiles[fd]) == NULL)
2812 if (fp->f_type != DTYPE_VNODE && fp->f_type != DTYPE_FIFO)
2818 * getfh_args(char *fname, fhandle_t *fhp)
2820 * Get (NFS) file handle
2823 getfh(struct getfh_args *uap)
2825 struct thread *td = curthread;
2826 struct nlookupdata nd;
2832 * Must be super user
2834 if ((error = suser(td)) != 0)
2838 error = nlookup_init(&nd, uap->fname, UIO_USERSPACE, NLC_FOLLOW);
2840 error = nlookup(&nd);
2842 error = cache_vget(nd.nl_ncp, nd.nl_cred, LK_EXCLUSIVE, &vp);
2845 bzero(&fh, sizeof(fh));
2846 fh.fh_fsid = vp->v_mount->mnt_stat.f_fsid;
2847 error = VFS_VPTOFH(vp, &fh.fh_fid);
2850 error = copyout(&fh, uap->fhp, sizeof(fh));
2856 * fhopen_args(const struct fhandle *u_fhp, int flags)
2858 * syscall for the rpc.lockd to use to translate a NFS file handle into
2859 * an open descriptor.
2861 * warning: do not remove the suser() call or this becomes one giant
2865 fhopen(struct fhopen_args *uap)
2867 struct thread *td = curthread;
2868 struct proc *p = td->td_proc;
2873 struct vattr *vap = &vat;
2875 struct filedesc *fdp = p->p_fd;
2876 int fmode, mode, error, type;
2882 * Must be super user
2888 fmode = FFLAGS(SCARG(uap, flags));
2889 /* why not allow a non-read/write open for our lockd? */
2890 if (((fmode & (FREAD | FWRITE)) == 0) || (fmode & O_CREAT))
2892 error = copyin(SCARG(uap,u_fhp), &fhp, sizeof(fhp));
2895 /* find the mount point */
2896 mp = vfs_getvfs(&fhp.fh_fsid);
2899 /* now give me my vnode, it gets returned to me locked */
2900 error = VFS_FHTOVP(mp, &fhp.fh_fid, &vp);
2904 * from now on we have to make sure not
2905 * to forget about the vnode
2906 * any error that causes an abort must vput(vp)
2907 * just set error = err and 'goto bad;'.
2913 if (vp->v_type == VLNK) {
2917 if (vp->v_type == VSOCK) {
2922 if (fmode & (FWRITE | O_TRUNC)) {
2923 if (vp->v_type == VDIR) {
2927 error = vn_writechk(vp);
2935 error = VOP_ACCESS(vp, mode, p->p_ucred, td);
2939 if (fmode & O_TRUNC) {
2940 VOP_UNLOCK(vp, 0, td); /* XXX */
2941 VOP_LEASE(vp, td, p->p_ucred, LEASE_WRITE);
2942 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY, td); /* XXX */
2945 error = VOP_SETATTR(vp, vap, p->p_ucred, td);
2951 * VOP_OPEN needs the file pointer so it can potentially override
2954 * WARNING! no f_ncp will be associated when fhopen()ing a directory.
2957 if ((error = falloc(p, &nfp, NULL)) != 0)
2961 fp->f_data = (caddr_t)vp;
2962 fp->f_flag = fmode & FMASK;
2963 fp->f_ops = &vnode_fileops;
2964 fp->f_type = DTYPE_VNODE;
2966 error = VOP_OPEN(vp, fmode, p->p_ucred, fp, td);
2969 * setting f_ops this way prevents VOP_CLOSE from being
2970 * called or fdrop() releasing the vp from v_data. Since
2971 * the VOP_OPEN failed we don't want to VOP_CLOSE.
2973 fp->f_ops = &badfileops;
2982 * The fp now owns a reference on the vnode. We still have our own
2988 * Make sure that a VM object is created for VMIO support. If this
2989 * fails just fdrop() normally to clean up.
2991 if (vn_canvmio(vp) == TRUE) {
2992 if ((error = vfs_object_create(vp, td)) != 0) {
2999 * The open was successful, associate it with a file descriptor.
3001 if ((error = fsetfd(p, fp, &indx)) != 0) {
3008 if (fmode & (O_EXLOCK | O_SHLOCK)) {
3009 lf.l_whence = SEEK_SET;
3012 if (fmode & O_EXLOCK)
3013 lf.l_type = F_WRLCK;
3015 lf.l_type = F_RDLCK;
3017 if ((fmode & FNONBLOCK) == 0)
3019 VOP_UNLOCK(vp, 0, td);
3020 if ((error = VOP_ADVLOCK(vp, (caddr_t)fp, F_SETLK, &lf, type)) != 0) {
3022 * lock request failed. Normally close the descriptor
3023 * but handle the case where someone might have dup()d
3024 * or close()d it when we weren't looking.
3026 if (fdp->fd_ofiles[indx] == fp) {
3027 fdp->fd_ofiles[indx] = NULL;
3032 * release our private reference.
3038 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY, td);
3039 fp->f_flag |= FHASLOCK;
3041 if ((vp->v_type == VREG) && (VOP_GETVOBJECT(vp, NULL) != 0))
3042 vfs_object_create(vp, td);
3046 uap->sysmsg_result = indx;
3055 * fhstat_args(struct fhandle *u_fhp, struct stat *sb)
3058 fhstat(struct fhstat_args *uap)
3060 struct thread *td = curthread;
3068 * Must be super user
3074 error = copyin(SCARG(uap, u_fhp), &fh, sizeof(fhandle_t));
3078 if ((mp = vfs_getvfs(&fh.fh_fsid)) == NULL)
3080 if ((error = VFS_FHTOVP(mp, &fh.fh_fid, &vp)))
3082 error = vn_stat(vp, &sb, td);
3086 error = copyout(&sb, SCARG(uap, sb), sizeof(sb));
3091 * fhstatfs_args(struct fhandle *u_fhp, struct statfs *buf)
3094 fhstatfs(struct fhstatfs_args *uap)
3096 struct thread *td = curthread;
3105 * Must be super user
3107 if ((error = suser(td)))
3110 if ((error = copyin(SCARG(uap, u_fhp), &fh, sizeof(fhandle_t))) != 0)
3113 if ((mp = vfs_getvfs(&fh.fh_fsid)) == NULL)
3115 if ((error = VFS_FHTOVP(mp, &fh.fh_fid, &vp)))
3120 if ((error = VFS_STATFS(mp, sp, td)) != 0)
3122 sp->f_flags = mp->mnt_flag & MNT_VISFLAGMASK;
3124 bcopy(sp, &sb, sizeof(sb));
3125 sb.f_fsid.val[0] = sb.f_fsid.val[1] = 0;
3128 return (copyout(sp, SCARG(uap, buf), sizeof(*sp)));
3132 * Syscall to push extended attribute configuration information into the
3133 * VFS. Accepts a path, which it converts to a mountpoint, as well as
3134 * a command (int cmd), and attribute name and misc data. For now, the
3135 * attribute name is left in userspace for consumption by the VFS_op.
3136 * It will probably be changed to be copied into sysspace by the
3137 * syscall in the future, once issues with various consumers of the
3138 * attribute code have raised their hands.
3140 * Currently this is used only by UFS Extended Attributes.
3143 extattrctl(struct extattrctl_args *uap)
3145 struct nlookupdata nd;
3151 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
3153 error = nlookup(&nd);
3155 mp = nd.nl_ncp->nc_mount;
3156 error = VFS_EXTATTRCTL(mp, SCARG(uap, cmd),
3157 SCARG(uap, attrname), SCARG(uap, arg),
3165 * Syscall to set a named extended attribute on a file or directory.
3166 * Accepts attribute name, and a uio structure pointing to the data to set.
3167 * The uio is consumed in the style of writev(). The real work happens
3168 * in VOP_SETEXTATTR().
3171 extattr_set_file(struct extattr_set_file_args *uap)
3173 char attrname[EXTATTR_MAXNAMELEN];
3174 struct iovec aiov[UIO_SMALLIOV];
3175 struct iovec *needfree;
3176 struct nlookupdata nd;
3185 error = copyin(SCARG(uap, attrname), attrname, EXTATTR_MAXNAMELEN);
3190 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
3192 error = nlookup(&nd);
3194 error = cache_vget(nd.nl_ncp, nd.nl_cred, LK_EXCLUSIVE, &vp);
3201 iovlen = uap->iovcnt * sizeof(struct iovec);
3202 if (uap->iovcnt > UIO_SMALLIOV) {
3203 if (uap->iovcnt > UIO_MAXIOV) {
3207 MALLOC(iov, struct iovec *, iovlen, M_IOV, M_WAITOK);
3213 auio.uio_iovcnt = uap->iovcnt;
3214 auio.uio_rw = UIO_WRITE;
3215 auio.uio_segflg = UIO_USERSPACE;
3216 auio.uio_td = nd.nl_td;
3217 auio.uio_offset = 0;
3218 if ((error = copyin(uap->iovp, iov, iovlen)))
3221 for (i = 0; i < uap->iovcnt; i++) {
3222 if (iov->iov_len > INT_MAX - auio.uio_resid) {
3226 auio.uio_resid += iov->iov_len;
3229 cnt = auio.uio_resid;
3230 error = VOP_SETEXTATTR(vp, attrname, &auio, nd.nl_cred, nd.nl_td);
3231 cnt -= auio.uio_resid;
3232 uap->sysmsg_result = cnt;
3237 FREE(needfree, M_IOV);
3242 * Syscall to get a named extended attribute on a file or directory.
3243 * Accepts attribute name, and a uio structure pointing to a buffer for the
3244 * data. The uio is consumed in the style of readv(). The real work
3245 * happens in VOP_GETEXTATTR();
3248 extattr_get_file(struct extattr_get_file_args *uap)
3250 char attrname[EXTATTR_MAXNAMELEN];
3251 struct iovec aiov[UIO_SMALLIOV];
3252 struct iovec *needfree;
3253 struct nlookupdata nd;
3262 error = copyin(SCARG(uap, attrname), attrname, EXTATTR_MAXNAMELEN);
3267 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
3269 error = nlookup(&nd);
3271 error = cache_vget(nd.nl_ncp, nd.nl_cred, LK_EXCLUSIVE, &vp);
3277 iovlen = uap->iovcnt * sizeof (struct iovec);
3279 if (uap->iovcnt > UIO_SMALLIOV) {
3280 if (uap->iovcnt > UIO_MAXIOV) {
3284 MALLOC(iov, struct iovec *, iovlen, M_IOV, M_WAITOK);
3290 auio.uio_iovcnt = uap->iovcnt;
3291 auio.uio_rw = UIO_READ;
3292 auio.uio_segflg = UIO_USERSPACE;
3293 auio.uio_td = nd.nl_td;
3294 auio.uio_offset = 0;
3295 if ((error = copyin(uap->iovp, iov, iovlen)))
3298 for (i = 0; i < uap->iovcnt; i++) {
3299 if (iov->iov_len > INT_MAX - auio.uio_resid) {
3303 auio.uio_resid += iov->iov_len;
3306 cnt = auio.uio_resid;
3307 error = VOP_GETEXTATTR(vp, attrname, &auio, nd.nl_cred, nd.nl_td);
3308 cnt -= auio.uio_resid;
3309 uap->sysmsg_result = cnt;
3314 FREE(needfree, M_IOV);
3319 * Syscall to delete a named extended attribute from a file or directory.
3320 * Accepts attribute name. The real work happens in VOP_SETEXTATTR().
3323 extattr_delete_file(struct extattr_delete_file_args *uap)
3325 char attrname[EXTATTR_MAXNAMELEN];
3326 struct nlookupdata nd;
3330 error = copyin(SCARG(uap, attrname), attrname, EXTATTR_MAXNAMELEN);
3335 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
3337 error = nlookup(&nd);
3339 error = cache_vget(nd.nl_ncp, nd.nl_cred, LK_EXCLUSIVE, &vp);
3345 error = VOP_SETEXTATTR(vp, attrname, NULL, nd.nl_cred, nd.nl_td);
3352 * print out statistics from the current status of the buffer pool
3353 * this can be toggeled by the system control option debug.syncprt
3362 int counts[(MAXBSIZE / PAGE_SIZE) + 1];
3363 static char *bname[3] = { "LOCKED", "LRU", "AGE" };
3365 for (dp = bufqueues, i = 0; dp < &bufqueues[3]; dp++, i++) {
3367 for (j = 0; j <= MAXBSIZE/PAGE_SIZE; j++)
3370 TAILQ_FOREACH(bp, dp, b_freelist) {
3371 counts[bp->b_bufsize/PAGE_SIZE]++;
3375 printf("%s: total-%d", bname[i], count);
3376 for (j = 0; j <= MAXBSIZE/PAGE_SIZE; j++)
3378 printf(", %d-%d", j * PAGE_SIZE, counts[j]);