Home | History | Annotate | Line # | Download | only in kern
vfs_syscalls.c revision 1.1.1.2
      1  1.1.1.2  fvdl /*
      2  1.1.1.2  fvdl  * Copyright (c) 1989, 1993
      3  1.1.1.2  fvdl  *	The Regents of the University of California.  All rights reserved.
      4  1.1.1.2  fvdl  * (c) UNIX System Laboratories, Inc.
      5  1.1.1.2  fvdl  * All or some portions of this file are derived from material licensed
      6  1.1.1.2  fvdl  * to the University of California by American Telephone and Telegraph
      7  1.1.1.2  fvdl  * Co. or Unix System Laboratories, Inc. and are reproduced herein with
      8  1.1.1.2  fvdl  * the permission of UNIX System Laboratories, Inc.
      9  1.1.1.2  fvdl  *
     10  1.1.1.2  fvdl  * Redistribution and use in source and binary forms, with or without
     11  1.1.1.2  fvdl  * modification, are permitted provided that the following conditions
     12  1.1.1.2  fvdl  * are met:
     13  1.1.1.2  fvdl  * 1. Redistributions of source code must retain the above copyright
     14  1.1.1.2  fvdl  *    notice, this list of conditions and the following disclaimer.
     15  1.1.1.2  fvdl  * 2. Redistributions in binary form must reproduce the above copyright
     16  1.1.1.2  fvdl  *    notice, this list of conditions and the following disclaimer in the
     17  1.1.1.2  fvdl  *    documentation and/or other materials provided with the distribution.
     18  1.1.1.2  fvdl  * 3. All advertising materials mentioning features or use of this software
     19  1.1.1.2  fvdl  *    must display the following acknowledgement:
     20  1.1.1.2  fvdl  *	This product includes software developed by the University of
     21  1.1.1.2  fvdl  *	California, Berkeley and its contributors.
     22  1.1.1.2  fvdl  * 4. Neither the name of the University nor the names of its contributors
     23  1.1.1.2  fvdl  *    may be used to endorse or promote products derived from this software
     24  1.1.1.2  fvdl  *    without specific prior written permission.
     25  1.1.1.2  fvdl  *
     26  1.1.1.2  fvdl  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     27  1.1.1.2  fvdl  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     28  1.1.1.2  fvdl  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     29  1.1.1.2  fvdl  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     30  1.1.1.2  fvdl  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     31  1.1.1.2  fvdl  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     32  1.1.1.2  fvdl  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     33  1.1.1.2  fvdl  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     34  1.1.1.2  fvdl  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     35  1.1.1.2  fvdl  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     36  1.1.1.2  fvdl  * SUCH DAMAGE.
     37  1.1.1.2  fvdl  *
     38  1.1.1.2  fvdl  *	@(#)vfs_syscalls.c	8.13 (Berkeley) 4/15/94
     39  1.1.1.2  fvdl  */
     40  1.1.1.2  fvdl 
     41  1.1.1.2  fvdl #include <sys/param.h>
     42  1.1.1.2  fvdl #include <sys/systm.h>
     43  1.1.1.2  fvdl #include <sys/namei.h>
     44  1.1.1.2  fvdl #include <sys/filedesc.h>
     45  1.1.1.2  fvdl #include <sys/kernel.h>
     46  1.1.1.2  fvdl #include <sys/file.h>
     47  1.1.1.2  fvdl #include <sys/stat.h>
     48  1.1.1.2  fvdl #include <sys/vnode.h>
     49  1.1.1.2  fvdl #include <sys/mount.h>
     50  1.1.1.2  fvdl #include <sys/proc.h>
     51  1.1.1.2  fvdl #include <sys/uio.h>
     52  1.1.1.2  fvdl #include <sys/malloc.h>
     53  1.1.1.2  fvdl #include <sys/dirent.h>
     54  1.1.1.2  fvdl 
     55  1.1.1.2  fvdl #include <vm/vm.h>
     56  1.1.1.2  fvdl #include <sys/sysctl.h>
     57  1.1.1.2  fvdl 
     58  1.1.1.2  fvdl static int change_dir __P((struct nameidata *ndp, struct proc *p));
     59  1.1.1.2  fvdl 
     60  1.1.1.2  fvdl /*
     61  1.1.1.2  fvdl  * Virtual File System System Calls
     62  1.1.1.2  fvdl  */
     63  1.1.1.2  fvdl 
     64  1.1.1.2  fvdl /*
     65  1.1.1.2  fvdl  * Mount a file system.
     66  1.1.1.2  fvdl  */
     67  1.1.1.2  fvdl struct mount_args {
     68  1.1.1.2  fvdl 	int	type;
     69  1.1.1.2  fvdl 	char	*path;
     70  1.1.1.2  fvdl 	int	flags;
     71  1.1.1.2  fvdl 	caddr_t	data;
     72  1.1.1.2  fvdl };
     73  1.1.1.2  fvdl /* ARGSUSED */
     74  1.1.1.2  fvdl mount(p, uap, retval)
     75  1.1.1.2  fvdl 	struct proc *p;
     76  1.1.1.2  fvdl 	register struct mount_args *uap;
     77  1.1.1.2  fvdl 	int *retval;
     78  1.1.1.2  fvdl {
     79  1.1.1.2  fvdl 	register struct vnode *vp;
     80  1.1.1.2  fvdl 	register struct mount *mp;
     81  1.1.1.2  fvdl 	int error, flag;
     82  1.1.1.2  fvdl 	struct nameidata nd;
     83  1.1.1.2  fvdl 
     84  1.1.1.2  fvdl 	/*
     85  1.1.1.2  fvdl 	 * Must be super user
     86  1.1.1.2  fvdl 	 */
     87  1.1.1.2  fvdl 	if (error = suser(p->p_ucred, &p->p_acflag))
     88  1.1.1.2  fvdl 		return (error);
     89  1.1.1.2  fvdl 	/*
     90  1.1.1.2  fvdl 	 * Get vnode to be covered
     91  1.1.1.2  fvdl 	 */
     92  1.1.1.2  fvdl 	NDINIT(&nd, LOOKUP, FOLLOW | LOCKLEAF, UIO_USERSPACE, uap->path, p);
     93  1.1.1.2  fvdl 	if (error = namei(&nd))
     94  1.1.1.2  fvdl 		return (error);
     95  1.1.1.2  fvdl 	vp = nd.ni_vp;
     96  1.1.1.2  fvdl 	if (uap->flags & MNT_UPDATE) {
     97  1.1.1.2  fvdl 		if ((vp->v_flag & VROOT) == 0) {
     98  1.1.1.2  fvdl 			vput(vp);
     99  1.1.1.2  fvdl 			return (EINVAL);
    100  1.1.1.2  fvdl 		}
    101  1.1.1.2  fvdl 		mp = vp->v_mount;
    102  1.1.1.2  fvdl 		flag = mp->mnt_flag;
    103  1.1.1.2  fvdl 		/*
    104  1.1.1.2  fvdl 		 * We only allow the filesystem to be reloaded if it
    105  1.1.1.2  fvdl 		 * is currently mounted read-only.
    106  1.1.1.2  fvdl 		 */
    107  1.1.1.2  fvdl 		if ((uap->flags & MNT_RELOAD) &&
    108  1.1.1.2  fvdl 		    ((mp->mnt_flag & MNT_RDONLY) == 0)) {
    109  1.1.1.2  fvdl 			vput(vp);
    110  1.1.1.2  fvdl 			return (EOPNOTSUPP);	/* Needs translation */
    111  1.1.1.2  fvdl 		}
    112  1.1.1.2  fvdl 		mp->mnt_flag |=
    113  1.1.1.2  fvdl 		    uap->flags & (MNT_RELOAD | MNT_FORCE | MNT_UPDATE);
    114  1.1.1.2  fvdl 		VOP_UNLOCK(vp);
    115  1.1.1.2  fvdl 		goto update;
    116  1.1.1.2  fvdl 	}
    117  1.1.1.2  fvdl 	if (error = vinvalbuf(vp, V_SAVE, p->p_ucred, p, 0, 0))
    118  1.1.1.2  fvdl 		return (error);
    119  1.1.1.2  fvdl 	if (vp->v_type != VDIR) {
    120  1.1.1.2  fvdl 		vput(vp);
    121  1.1.1.2  fvdl 		return (ENOTDIR);
    122  1.1.1.2  fvdl 	}
    123  1.1.1.2  fvdl 	if ((u_long)uap->type > MOUNT_MAXTYPE || vfssw[uap->type] == NULL) {
    124  1.1.1.2  fvdl 		vput(vp);
    125  1.1.1.2  fvdl 		return (ENODEV);
    126  1.1.1.2  fvdl 	}
    127  1.1.1.2  fvdl 
    128  1.1.1.2  fvdl 	/*
    129  1.1.1.2  fvdl 	 * Allocate and initialize the file system.
    130  1.1.1.2  fvdl 	 */
    131  1.1.1.2  fvdl 	mp = (struct mount *)malloc((u_long)sizeof(struct mount),
    132  1.1.1.2  fvdl 		M_MOUNT, M_WAITOK);
    133  1.1.1.2  fvdl 	bzero((char *)mp, (u_long)sizeof(struct mount));
    134  1.1.1.2  fvdl 	mp->mnt_op = vfssw[uap->type];
    135  1.1.1.2  fvdl 	if (error = vfs_lock(mp)) {
    136  1.1.1.2  fvdl 		free((caddr_t)mp, M_MOUNT);
    137  1.1.1.2  fvdl 		vput(vp);
    138  1.1.1.2  fvdl 		return (error);
    139  1.1.1.2  fvdl 	}
    140  1.1.1.2  fvdl 	if (vp->v_mountedhere != NULL) {
    141  1.1.1.2  fvdl 		vfs_unlock(mp);
    142  1.1.1.2  fvdl 		free((caddr_t)mp, M_MOUNT);
    143  1.1.1.2  fvdl 		vput(vp);
    144  1.1.1.2  fvdl 		return (EBUSY);
    145  1.1.1.2  fvdl 	}
    146  1.1.1.2  fvdl 	vp->v_mountedhere = mp;
    147  1.1.1.2  fvdl 	mp->mnt_vnodecovered = vp;
    148  1.1.1.2  fvdl update:
    149  1.1.1.2  fvdl 	/*
    150  1.1.1.2  fvdl 	 * Set the mount level flags.
    151  1.1.1.2  fvdl 	 */
    152  1.1.1.2  fvdl 	if (uap->flags & MNT_RDONLY)
    153  1.1.1.2  fvdl 		mp->mnt_flag |= MNT_RDONLY;
    154  1.1.1.2  fvdl 	else if (mp->mnt_flag & MNT_RDONLY)
    155  1.1.1.2  fvdl 		mp->mnt_flag |= MNT_WANTRDWR;
    156  1.1.1.2  fvdl 	mp->mnt_flag &=~ (MNT_NOSUID | MNT_NOEXEC | MNT_NODEV |
    157  1.1.1.2  fvdl 	    MNT_SYNCHRONOUS | MNT_UNION | MNT_ASYNC);
    158  1.1.1.2  fvdl 	mp->mnt_flag |= uap->flags & (MNT_NOSUID | MNT_NOEXEC | MNT_NODEV |
    159  1.1.1.2  fvdl 	    MNT_SYNCHRONOUS | MNT_UNION | MNT_ASYNC);
    160  1.1.1.2  fvdl 	/*
    161  1.1.1.2  fvdl 	 * Mount the filesystem.
    162  1.1.1.2  fvdl 	 */
    163  1.1.1.2  fvdl 	error = VFS_MOUNT(mp, uap->path, uap->data, &nd, p);
    164  1.1.1.2  fvdl 	if (mp->mnt_flag & MNT_UPDATE) {
    165  1.1.1.2  fvdl 		vrele(vp);
    166  1.1.1.2  fvdl 		if (mp->mnt_flag & MNT_WANTRDWR)
    167  1.1.1.2  fvdl 			mp->mnt_flag &= ~MNT_RDONLY;
    168  1.1.1.2  fvdl 		mp->mnt_flag &=~
    169  1.1.1.2  fvdl 		    (MNT_UPDATE | MNT_RELOAD | MNT_FORCE | MNT_WANTRDWR);
    170  1.1.1.2  fvdl 		if (error)
    171  1.1.1.2  fvdl 			mp->mnt_flag = flag;
    172  1.1.1.2  fvdl 		return (error);
    173  1.1.1.2  fvdl 	}
    174  1.1.1.2  fvdl 	/*
    175  1.1.1.2  fvdl 	 * Put the new filesystem on the mount list after root.
    176  1.1.1.2  fvdl 	 */
    177  1.1.1.2  fvdl 	cache_purge(vp);
    178  1.1.1.2  fvdl 	if (!error) {
    179  1.1.1.2  fvdl 		TAILQ_INSERT_TAIL(&mountlist, mp, mnt_list);
    180  1.1.1.2  fvdl 		VOP_UNLOCK(vp);
    181  1.1.1.2  fvdl 		vfs_unlock(mp);
    182  1.1.1.2  fvdl 		error = VFS_START(mp, 0, p);
    183  1.1.1.2  fvdl 	} else {
    184  1.1.1.2  fvdl 		mp->mnt_vnodecovered->v_mountedhere = (struct mount *)0;
    185  1.1.1.2  fvdl 		vfs_unlock(mp);
    186  1.1.1.2  fvdl 		free((caddr_t)mp, M_MOUNT);
    187  1.1.1.2  fvdl 		vput(vp);
    188  1.1.1.2  fvdl 	}
    189  1.1.1.2  fvdl 	return (error);
    190  1.1.1.2  fvdl }
    191  1.1.1.2  fvdl 
    192  1.1.1.2  fvdl /*
    193  1.1.1.2  fvdl  * Unmount a file system.
    194  1.1.1.2  fvdl  *
    195  1.1.1.2  fvdl  * Note: unmount takes a path to the vnode mounted on as argument,
    196  1.1.1.2  fvdl  * not special file (as before).
    197  1.1.1.2  fvdl  */
    198  1.1.1.2  fvdl struct unmount_args {
    199  1.1.1.2  fvdl 	char	*path;
    200  1.1.1.2  fvdl 	int	flags;
    201  1.1.1.2  fvdl };
    202  1.1.1.2  fvdl /* ARGSUSED */
    203  1.1.1.2  fvdl unmount(p, uap, retval)
    204  1.1.1.2  fvdl 	struct proc *p;
    205  1.1.1.2  fvdl 	register struct unmount_args *uap;
    206  1.1.1.2  fvdl 	int *retval;
    207  1.1.1.2  fvdl {
    208  1.1.1.2  fvdl 	register struct vnode *vp;
    209  1.1.1.2  fvdl 	struct mount *mp;
    210  1.1.1.2  fvdl 	int error;
    211  1.1.1.2  fvdl 	struct nameidata nd;
    212  1.1.1.2  fvdl 
    213  1.1.1.2  fvdl 	NDINIT(&nd, LOOKUP, FOLLOW | LOCKLEAF, UIO_USERSPACE, uap->path, p);
    214  1.1.1.2  fvdl 	if (error = namei(&nd))
    215  1.1.1.2  fvdl 		return (error);
    216  1.1.1.2  fvdl 	vp = nd.ni_vp;
    217  1.1.1.2  fvdl 
    218  1.1.1.2  fvdl 	/*
    219  1.1.1.2  fvdl 	 * Unless this is a user mount, then must
    220  1.1.1.2  fvdl 	 * have suser privilege.
    221  1.1.1.2  fvdl 	 */
    222  1.1.1.2  fvdl 	if (((vp->v_mount->mnt_flag & MNT_USER) == 0) &&
    223  1.1.1.2  fvdl 	    (error = suser(p->p_ucred, &p->p_acflag))) {
    224  1.1.1.2  fvdl 		vput(vp);
    225  1.1.1.2  fvdl 		return (error);
    226  1.1.1.2  fvdl 	}
    227  1.1.1.2  fvdl 
    228  1.1.1.2  fvdl 	/*
    229  1.1.1.2  fvdl 	 * Must be the root of the filesystem
    230  1.1.1.2  fvdl 	 */
    231  1.1.1.2  fvdl 	if ((vp->v_flag & VROOT) == 0) {
    232  1.1.1.2  fvdl 		vput(vp);
    233  1.1.1.2  fvdl 		return (EINVAL);
    234  1.1.1.2  fvdl 	}
    235  1.1.1.2  fvdl 	mp = vp->v_mount;
    236  1.1.1.2  fvdl 	vput(vp);
    237  1.1.1.2  fvdl 	return (dounmount(mp, uap->flags, p));
    238  1.1.1.2  fvdl }
    239  1.1.1.2  fvdl 
    240  1.1.1.2  fvdl /*
    241  1.1.1.2  fvdl  * Do the actual file system unmount.
    242  1.1.1.2  fvdl  */
    243  1.1.1.2  fvdl dounmount(mp, flags, p)
    244  1.1.1.2  fvdl 	register struct mount *mp;
    245  1.1.1.2  fvdl 	int flags;
    246  1.1.1.2  fvdl 	struct proc *p;
    247  1.1.1.2  fvdl {
    248  1.1.1.2  fvdl 	struct vnode *coveredvp;
    249  1.1.1.2  fvdl 	int error;
    250  1.1.1.2  fvdl 
    251  1.1.1.2  fvdl 	coveredvp = mp->mnt_vnodecovered;
    252  1.1.1.2  fvdl 	if (vfs_busy(mp))
    253  1.1.1.2  fvdl 		return (EBUSY);
    254  1.1.1.2  fvdl 	mp->mnt_flag |= MNT_UNMOUNT;
    255  1.1.1.2  fvdl 	if (error = vfs_lock(mp))
    256  1.1.1.2  fvdl 		return (error);
    257  1.1.1.2  fvdl 
    258  1.1.1.2  fvdl 	mp->mnt_flag &=~ MNT_ASYNC;
    259  1.1.1.2  fvdl 	vnode_pager_umount(mp);	/* release cached vnodes */
    260  1.1.1.2  fvdl 	cache_purgevfs(mp);	/* remove cache entries for this file sys */
    261  1.1.1.2  fvdl 	if ((error = VFS_SYNC(mp, MNT_WAIT, p->p_ucred, p)) == 0 ||
    262  1.1.1.2  fvdl 	    (flags & MNT_FORCE))
    263  1.1.1.2  fvdl 		error = VFS_UNMOUNT(mp, flags, p);
    264  1.1.1.2  fvdl 	mp->mnt_flag &= ~MNT_UNMOUNT;
    265  1.1.1.2  fvdl 	vfs_unbusy(mp);
    266  1.1.1.2  fvdl 	if (error) {
    267  1.1.1.2  fvdl 		vfs_unlock(mp);
    268  1.1.1.2  fvdl 	} else {
    269  1.1.1.2  fvdl 		vrele(coveredvp);
    270  1.1.1.2  fvdl 		TAILQ_REMOVE(&mountlist, mp, mnt_list);
    271  1.1.1.2  fvdl 		mp->mnt_vnodecovered->v_mountedhere = (struct mount *)0;
    272  1.1.1.2  fvdl 		vfs_unlock(mp);
    273  1.1.1.2  fvdl 		if (mp->mnt_vnodelist.lh_first != NULL)
    274  1.1.1.2  fvdl 			panic("unmount: dangling vnode");
    275  1.1.1.2  fvdl 		free((caddr_t)mp, M_MOUNT);
    276  1.1.1.2  fvdl 	}
    277  1.1.1.2  fvdl 	return (error);
    278  1.1.1.2  fvdl }
    279  1.1.1.2  fvdl 
    280  1.1.1.2  fvdl /*
    281  1.1.1.2  fvdl  * Sync each mounted filesystem.
    282  1.1.1.2  fvdl  */
    283  1.1.1.2  fvdl #ifdef DIAGNOSTIC
    284  1.1.1.2  fvdl int syncprt = 0;
    285  1.1.1.2  fvdl struct ctldebug debug0 = { "syncprt", &syncprt };
    286  1.1.1.2  fvdl #endif
    287  1.1.1.2  fvdl 
    288  1.1.1.2  fvdl struct sync_args {
    289  1.1.1.2  fvdl 	int	dummy;
    290  1.1.1.2  fvdl };
    291  1.1.1.2  fvdl /* ARGSUSED */
    292  1.1.1.2  fvdl sync(p, uap, retval)
    293  1.1.1.2  fvdl 	struct proc *p;
    294  1.1.1.2  fvdl 	struct sync_args *uap;
    295  1.1.1.2  fvdl 	int *retval;
    296  1.1.1.2  fvdl {
    297  1.1.1.2  fvdl 	register struct mount *mp, *nmp;
    298  1.1.1.2  fvdl 	int asyncflag;
    299  1.1.1.2  fvdl 
    300  1.1.1.2  fvdl 	for (mp = mountlist.tqh_first; mp != NULL; mp = nmp) {
    301  1.1.1.2  fvdl 		nmp = mp->mnt_list.tqe_next;
    302  1.1.1.2  fvdl 		/*
    303  1.1.1.2  fvdl 		 * The lock check below is to avoid races with mount
    304  1.1.1.2  fvdl 		 * and unmount.
    305  1.1.1.2  fvdl 		 */
    306  1.1.1.2  fvdl 		if ((mp->mnt_flag & (MNT_MLOCK|MNT_RDONLY|MNT_MPBUSY)) == 0 &&
    307  1.1.1.2  fvdl 		    !vfs_busy(mp)) {
    308  1.1.1.2  fvdl 			asyncflag = mp->mnt_flag & MNT_ASYNC;
    309  1.1.1.2  fvdl 			mp->mnt_flag &= ~MNT_ASYNC;
    310  1.1.1.2  fvdl 			VFS_SYNC(mp, MNT_NOWAIT, p->p_ucred, p);
    311  1.1.1.2  fvdl 			if (asyncflag)
    312  1.1.1.2  fvdl 				mp->mnt_flag |= MNT_ASYNC;
    313  1.1.1.2  fvdl 			vfs_unbusy(mp);
    314  1.1.1.2  fvdl 		}
    315  1.1.1.2  fvdl 	}
    316  1.1.1.2  fvdl #ifdef DIAGNOSTIC
    317  1.1.1.2  fvdl 	if (syncprt)
    318  1.1.1.2  fvdl 		vfs_bufstats();
    319  1.1.1.2  fvdl #endif /* DIAGNOSTIC */
    320  1.1.1.2  fvdl 	return (0);
    321  1.1.1.2  fvdl }
    322  1.1.1.2  fvdl 
    323  1.1.1.2  fvdl /*
    324  1.1.1.2  fvdl  * Change filesystem quotas.
    325  1.1.1.2  fvdl  */
    326  1.1.1.2  fvdl struct quotactl_args {
    327  1.1.1.2  fvdl 	char *path;
    328  1.1.1.2  fvdl 	int cmd;
    329  1.1.1.2  fvdl 	int uid;
    330  1.1.1.2  fvdl 	caddr_t arg;
    331  1.1.1.2  fvdl };
    332  1.1.1.2  fvdl /* ARGSUSED */
    333  1.1.1.2  fvdl quotactl(p, uap, retval)
    334  1.1.1.2  fvdl 	struct proc *p;
    335  1.1.1.2  fvdl 	register struct quotactl_args *uap;
    336  1.1.1.2  fvdl 	int *retval;
    337  1.1.1.2  fvdl {
    338  1.1.1.2  fvdl 	register struct mount *mp;
    339  1.1.1.2  fvdl 	int error;
    340  1.1.1.2  fvdl 	struct nameidata nd;
    341  1.1.1.2  fvdl 
    342  1.1.1.2  fvdl 	NDINIT(&nd, LOOKUP, FOLLOW, UIO_USERSPACE, uap->path, p);
    343  1.1.1.2  fvdl 	if (error = namei(&nd))
    344  1.1.1.2  fvdl 		return (error);
    345  1.1.1.2  fvdl 	mp = nd.ni_vp->v_mount;
    346  1.1.1.2  fvdl 	vrele(nd.ni_vp);
    347  1.1.1.2  fvdl 	return (VFS_QUOTACTL(mp, uap->cmd, uap->uid, uap->arg, p));
    348  1.1.1.2  fvdl }
    349  1.1.1.2  fvdl 
    350  1.1.1.2  fvdl /*
    351  1.1.1.2  fvdl  * Get filesystem statistics.
    352  1.1.1.2  fvdl  */
    353  1.1.1.2  fvdl struct statfs_args {
    354  1.1.1.2  fvdl 	char *path;
    355  1.1.1.2  fvdl 	struct statfs *buf;
    356  1.1.1.2  fvdl };
    357  1.1.1.2  fvdl /* ARGSUSED */
    358  1.1.1.2  fvdl statfs(p, uap, retval)
    359  1.1.1.2  fvdl 	struct proc *p;
    360  1.1.1.2  fvdl 	register struct statfs_args *uap;
    361  1.1.1.2  fvdl 	int *retval;
    362  1.1.1.2  fvdl {
    363  1.1.1.2  fvdl 	register struct mount *mp;
    364  1.1.1.2  fvdl 	register struct statfs *sp;
    365  1.1.1.2  fvdl 	int error;
    366  1.1.1.2  fvdl 	struct nameidata nd;
    367  1.1.1.2  fvdl 
    368  1.1.1.2  fvdl 	NDINIT(&nd, LOOKUP, FOLLOW, UIO_USERSPACE, uap->path, p);
    369  1.1.1.2  fvdl 	if (error = namei(&nd))
    370  1.1.1.2  fvdl 		return (error);
    371  1.1.1.2  fvdl 	mp = nd.ni_vp->v_mount;
    372  1.1.1.2  fvdl 	sp = &mp->mnt_stat;
    373  1.1.1.2  fvdl 	vrele(nd.ni_vp);
    374  1.1.1.2  fvdl 	if (error = VFS_STATFS(mp, sp, p))
    375  1.1.1.2  fvdl 		return (error);
    376  1.1.1.2  fvdl 	sp->f_flags = mp->mnt_flag & MNT_VISFLAGMASK;
    377  1.1.1.2  fvdl 	return (copyout((caddr_t)sp, (caddr_t)uap->buf, sizeof(*sp)));
    378  1.1.1.2  fvdl }
    379  1.1.1.2  fvdl 
    380  1.1.1.2  fvdl /*
    381  1.1.1.2  fvdl  * Get filesystem statistics.
    382  1.1.1.2  fvdl  */
    383  1.1.1.2  fvdl struct fstatfs_args {
    384  1.1.1.2  fvdl 	int fd;
    385  1.1.1.2  fvdl 	struct statfs *buf;
    386  1.1.1.2  fvdl };
    387  1.1.1.2  fvdl /* ARGSUSED */
    388  1.1.1.2  fvdl fstatfs(p, uap, retval)
    389  1.1.1.2  fvdl 	struct proc *p;
    390  1.1.1.2  fvdl 	register struct fstatfs_args *uap;
    391  1.1.1.2  fvdl 	int *retval;
    392  1.1.1.2  fvdl {
    393  1.1.1.2  fvdl 	struct file *fp;
    394  1.1.1.2  fvdl 	struct mount *mp;
    395  1.1.1.2  fvdl 	register struct statfs *sp;
    396  1.1.1.2  fvdl 	int error;
    397  1.1.1.2  fvdl 
    398  1.1.1.2  fvdl 	if (error = getvnode(p->p_fd, uap->fd, &fp))
    399  1.1.1.2  fvdl 		return (error);
    400  1.1.1.2  fvdl 	mp = ((struct vnode *)fp->f_data)->v_mount;
    401  1.1.1.2  fvdl 	sp = &mp->mnt_stat;
    402  1.1.1.2  fvdl 	if (error = VFS_STATFS(mp, sp, p))
    403  1.1.1.2  fvdl 		return (error);
    404  1.1.1.2  fvdl 	sp->f_flags = mp->mnt_flag & MNT_VISFLAGMASK;
    405  1.1.1.2  fvdl 	return (copyout((caddr_t)sp, (caddr_t)uap->buf, sizeof(*sp)));
    406  1.1.1.2  fvdl }
    407  1.1.1.2  fvdl 
    408  1.1.1.2  fvdl /*
    409  1.1.1.2  fvdl  * Get statistics on all filesystems.
    410  1.1.1.2  fvdl  */
    411  1.1.1.2  fvdl struct getfsstat_args {
    412  1.1.1.2  fvdl 	struct statfs *buf;
    413  1.1.1.2  fvdl 	long bufsize;
    414  1.1.1.2  fvdl 	int flags;
    415  1.1.1.2  fvdl };
    416  1.1.1.2  fvdl getfsstat(p, uap, retval)
    417  1.1.1.2  fvdl 	struct proc *p;
    418  1.1.1.2  fvdl 	register struct getfsstat_args *uap;
    419  1.1.1.2  fvdl 	int *retval;
    420  1.1.1.2  fvdl {
    421  1.1.1.2  fvdl 	register struct mount *mp, *nmp;
    422  1.1.1.2  fvdl 	register struct statfs *sp;
    423  1.1.1.2  fvdl 	caddr_t sfsp;
    424  1.1.1.2  fvdl 	long count, maxcount, error;
    425  1.1.1.2  fvdl 
    426  1.1.1.2  fvdl 	maxcount = uap->bufsize / sizeof(struct statfs);
    427  1.1.1.2  fvdl 	sfsp = (caddr_t)uap->buf;
    428  1.1.1.2  fvdl 	for (count = 0, mp = mountlist.tqh_first; mp != NULL; mp = nmp) {
    429  1.1.1.2  fvdl 		nmp = mp->mnt_list.tqe_next;
    430  1.1.1.2  fvdl 		if (sfsp && count < maxcount &&
    431  1.1.1.2  fvdl 		    ((mp->mnt_flag & MNT_MLOCK) == 0)) {
    432  1.1.1.2  fvdl 			sp = &mp->mnt_stat;
    433  1.1.1.2  fvdl 			/*
    434  1.1.1.2  fvdl 			 * If MNT_NOWAIT is specified, do not refresh the
    435  1.1.1.2  fvdl 			 * fsstat cache. MNT_WAIT overrides MNT_NOWAIT.
    436  1.1.1.2  fvdl 			 */
    437  1.1.1.2  fvdl 			if (((uap->flags & MNT_NOWAIT) == 0 ||
    438  1.1.1.2  fvdl 			    (uap->flags & MNT_WAIT)) &&
    439  1.1.1.2  fvdl 			    (error = VFS_STATFS(mp, sp, p)))
    440  1.1.1.2  fvdl 				continue;
    441  1.1.1.2  fvdl 			sp->f_flags = mp->mnt_flag & MNT_VISFLAGMASK;
    442  1.1.1.2  fvdl 			if (error = copyout((caddr_t)sp, sfsp, sizeof(*sp)))
    443  1.1.1.2  fvdl 				return (error);
    444  1.1.1.2  fvdl 			sfsp += sizeof(*sp);
    445  1.1.1.2  fvdl 		}
    446  1.1.1.2  fvdl 		count++;
    447  1.1.1.2  fvdl 	}
    448  1.1.1.2  fvdl 	if (sfsp && count > maxcount)
    449  1.1.1.2  fvdl 		*retval = maxcount;
    450  1.1.1.2  fvdl 	else
    451  1.1.1.2  fvdl 		*retval = count;
    452  1.1.1.2  fvdl 	return (0);
    453  1.1.1.2  fvdl }
    454  1.1.1.2  fvdl 
    455  1.1.1.2  fvdl /*
    456  1.1.1.2  fvdl  * Change current working directory to a given file descriptor.
    457  1.1.1.2  fvdl  */
    458  1.1.1.2  fvdl struct fchdir_args {
    459  1.1.1.2  fvdl 	int	fd;
    460  1.1.1.2  fvdl };
    461  1.1.1.2  fvdl /* ARGSUSED */
    462  1.1.1.2  fvdl fchdir(p, uap, retval)
    463  1.1.1.2  fvdl 	struct proc *p;
    464  1.1.1.2  fvdl 	struct fchdir_args *uap;
    465  1.1.1.2  fvdl 	int *retval;
    466  1.1.1.2  fvdl {
    467  1.1.1.2  fvdl 	register struct filedesc *fdp = p->p_fd;
    468  1.1.1.2  fvdl 	register struct vnode *vp;
    469  1.1.1.2  fvdl 	struct file *fp;
    470  1.1.1.2  fvdl 	int error;
    471  1.1.1.2  fvdl 
    472  1.1.1.2  fvdl 	if (error = getvnode(fdp, uap->fd, &fp))
    473  1.1.1.2  fvdl 		return (error);
    474  1.1.1.2  fvdl 	vp = (struct vnode *)fp->f_data;
    475  1.1.1.2  fvdl 	VOP_LOCK(vp);
    476  1.1.1.2  fvdl 	if (vp->v_type != VDIR)
    477  1.1.1.2  fvdl 		error = ENOTDIR;
    478  1.1.1.2  fvdl 	else
    479  1.1.1.2  fvdl 		error = VOP_ACCESS(vp, VEXEC, p->p_ucred, p);
    480  1.1.1.2  fvdl 	VOP_UNLOCK(vp);
    481  1.1.1.2  fvdl 	if (error)
    482  1.1.1.2  fvdl 		return (error);
    483  1.1.1.2  fvdl 	VREF(vp);
    484  1.1.1.2  fvdl 	vrele(fdp->fd_cdir);
    485  1.1.1.2  fvdl 	fdp->fd_cdir = vp;
    486  1.1.1.2  fvdl 	return (0);
    487  1.1.1.2  fvdl }
    488  1.1.1.2  fvdl 
    489  1.1.1.2  fvdl /*
    490  1.1.1.2  fvdl  * Change current working directory (``.'').
    491  1.1.1.2  fvdl  */
    492  1.1.1.2  fvdl struct chdir_args {
    493  1.1.1.2  fvdl 	char	*path;
    494  1.1.1.2  fvdl };
    495  1.1.1.2  fvdl /* ARGSUSED */
    496  1.1.1.2  fvdl chdir(p, uap, retval)
    497  1.1.1.2  fvdl 	struct proc *p;
    498  1.1.1.2  fvdl 	struct chdir_args *uap;
    499  1.1.1.2  fvdl 	int *retval;
    500  1.1.1.2  fvdl {
    501  1.1.1.2  fvdl 	register struct filedesc *fdp = p->p_fd;
    502  1.1.1.2  fvdl 	int error;
    503  1.1.1.2  fvdl 	struct nameidata nd;
    504  1.1.1.2  fvdl 
    505  1.1.1.2  fvdl 	NDINIT(&nd, LOOKUP, FOLLOW | LOCKLEAF, UIO_USERSPACE, uap->path, p);
    506  1.1.1.2  fvdl 	if (error = change_dir(&nd, p))
    507  1.1.1.2  fvdl 		return (error);
    508  1.1.1.2  fvdl 	vrele(fdp->fd_cdir);
    509  1.1.1.2  fvdl 	fdp->fd_cdir = nd.ni_vp;
    510  1.1.1.2  fvdl 	return (0);
    511  1.1.1.2  fvdl }
    512  1.1.1.2  fvdl 
    513  1.1.1.2  fvdl /*
    514  1.1.1.2  fvdl  * Change notion of root (``/'') directory.
    515  1.1.1.2  fvdl  */
    516  1.1.1.2  fvdl struct chroot_args {
    517  1.1.1.2  fvdl 	char	*path;
    518  1.1.1.2  fvdl };
    519  1.1.1.2  fvdl /* ARGSUSED */
    520  1.1.1.2  fvdl chroot(p, uap, retval)
    521  1.1.1.2  fvdl 	struct proc *p;
    522  1.1.1.2  fvdl 	struct chroot_args *uap;
    523  1.1.1.2  fvdl 	int *retval;
    524  1.1.1.2  fvdl {
    525  1.1.1.2  fvdl 	register struct filedesc *fdp = p->p_fd;
    526  1.1.1.2  fvdl 	int error;
    527  1.1.1.2  fvdl 	struct nameidata nd;
    528  1.1.1.2  fvdl 
    529  1.1.1.2  fvdl 	if (error = suser(p->p_ucred, &p->p_acflag))
    530  1.1.1.2  fvdl 		return (error);
    531  1.1.1.2  fvdl 	NDINIT(&nd, LOOKUP, FOLLOW | LOCKLEAF, UIO_USERSPACE, uap->path, p);
    532  1.1.1.2  fvdl 	if (error = change_dir(&nd, p))
    533  1.1.1.2  fvdl 		return (error);
    534  1.1.1.2  fvdl 	if (fdp->fd_rdir != NULL)
    535  1.1.1.2  fvdl 		vrele(fdp->fd_rdir);
    536  1.1.1.2  fvdl 	fdp->fd_rdir = nd.ni_vp;
    537  1.1.1.2  fvdl 	return (0);
    538  1.1.1.2  fvdl }
    539  1.1.1.2  fvdl 
    540  1.1.1.2  fvdl /*
    541  1.1.1.2  fvdl  * Common routine for chroot and chdir.
    542  1.1.1.2  fvdl  */
    543  1.1.1.2  fvdl static int
    544  1.1.1.2  fvdl change_dir(ndp, p)
    545  1.1.1.2  fvdl 	register struct nameidata *ndp;
    546  1.1.1.2  fvdl 	struct proc *p;
    547  1.1.1.2  fvdl {
    548  1.1.1.2  fvdl 	struct vnode *vp;
    549  1.1.1.2  fvdl 	int error;
    550  1.1.1.2  fvdl 
    551  1.1.1.2  fvdl 	if (error = namei(ndp))
    552  1.1.1.2  fvdl 		return (error);
    553  1.1.1.2  fvdl 	vp = ndp->ni_vp;
    554  1.1.1.2  fvdl 	if (vp->v_type != VDIR)
    555  1.1.1.2  fvdl 		error = ENOTDIR;
    556  1.1.1.2  fvdl 	else
    557  1.1.1.2  fvdl 		error = VOP_ACCESS(vp, VEXEC, p->p_ucred, p);
    558  1.1.1.2  fvdl 	VOP_UNLOCK(vp);
    559  1.1.1.2  fvdl 	if (error)
    560  1.1.1.2  fvdl 		vrele(vp);
    561  1.1.1.2  fvdl 	return (error);
    562  1.1.1.2  fvdl }
    563  1.1.1.2  fvdl 
    564  1.1.1.2  fvdl /*
    565  1.1.1.2  fvdl  * Check permissions, allocate an open file structure,
    566  1.1.1.2  fvdl  * and call the device open routine if any.
    567  1.1.1.2  fvdl  */
    568  1.1.1.2  fvdl struct open_args {
    569  1.1.1.2  fvdl 	char	*path;
    570  1.1.1.2  fvdl 	int	flags;
    571  1.1.1.2  fvdl 	int	mode;
    572  1.1.1.2  fvdl };
    573  1.1.1.2  fvdl open(p, uap, retval)
    574  1.1.1.2  fvdl 	struct proc *p;
    575  1.1.1.2  fvdl 	register struct open_args *uap;
    576  1.1.1.2  fvdl 	int *retval;
    577  1.1.1.2  fvdl {
    578  1.1.1.2  fvdl 	register struct filedesc *fdp = p->p_fd;
    579  1.1.1.2  fvdl 	register struct file *fp;
    580  1.1.1.2  fvdl 	register struct vnode *vp;
    581  1.1.1.2  fvdl 	int flags, cmode;
    582  1.1.1.2  fvdl 	struct file *nfp;
    583  1.1.1.2  fvdl 	int type, indx, error;
    584  1.1.1.2  fvdl 	struct flock lf;
    585  1.1.1.2  fvdl 	struct nameidata nd;
    586  1.1.1.2  fvdl 	extern struct fileops vnops;
    587  1.1.1.2  fvdl 
    588  1.1.1.2  fvdl 	if (error = falloc(p, &nfp, &indx))
    589  1.1.1.2  fvdl 		return (error);
    590  1.1.1.2  fvdl 	fp = nfp;
    591  1.1.1.2  fvdl 	flags = FFLAGS(uap->flags);
    592  1.1.1.2  fvdl 	cmode = ((uap->mode &~ fdp->fd_cmask) & ALLPERMS) &~ S_ISTXT;
    593  1.1.1.2  fvdl 	NDINIT(&nd, LOOKUP, FOLLOW, UIO_USERSPACE, uap->path, p);
    594  1.1.1.2  fvdl 	p->p_dupfd = -indx - 1;			/* XXX check for fdopen */
    595  1.1.1.2  fvdl 	if (error = vn_open(&nd, flags, cmode)) {
    596  1.1.1.2  fvdl 		ffree(fp);
    597  1.1.1.2  fvdl 		if ((error == ENODEV || error == ENXIO) &&
    598  1.1.1.2  fvdl 		    p->p_dupfd >= 0 && 			/* XXX from fdopen */
    599  1.1.1.2  fvdl 		    (error =
    600  1.1.1.2  fvdl 		        dupfdopen(fdp, indx, p->p_dupfd, flags, error)) == 0) {
    601  1.1.1.2  fvdl 			*retval = indx;
    602  1.1.1.2  fvdl 			return (0);
    603  1.1.1.2  fvdl 		}
    604  1.1.1.2  fvdl 		if (error == ERESTART)
    605  1.1.1.2  fvdl 			error = EINTR;
    606  1.1.1.2  fvdl 		fdp->fd_ofiles[indx] = NULL;
    607  1.1.1.2  fvdl 		return (error);
    608  1.1.1.2  fvdl 	}
    609  1.1.1.2  fvdl 	p->p_dupfd = 0;
    610  1.1.1.2  fvdl 	vp = nd.ni_vp;
    611  1.1.1.2  fvdl 	fp->f_flag = flags & FMASK;
    612  1.1.1.2  fvdl 	fp->f_type = DTYPE_VNODE;
    613  1.1.1.2  fvdl 	fp->f_ops = &vnops;
    614  1.1.1.2  fvdl 	fp->f_data = (caddr_t)vp;
    615  1.1.1.2  fvdl 	if (flags & (O_EXLOCK | O_SHLOCK)) {
    616  1.1.1.2  fvdl 		lf.l_whence = SEEK_SET;
    617  1.1.1.2  fvdl 		lf.l_start = 0;
    618  1.1.1.2  fvdl 		lf.l_len = 0;
    619  1.1.1.2  fvdl 		if (flags & O_EXLOCK)
    620  1.1.1.2  fvdl 			lf.l_type = F_WRLCK;
    621  1.1.1.2  fvdl 		else
    622  1.1.1.2  fvdl 			lf.l_type = F_RDLCK;
    623  1.1.1.2  fvdl 		type = F_FLOCK;
    624  1.1.1.2  fvdl 		if ((flags & FNONBLOCK) == 0)
    625  1.1.1.2  fvdl 			type |= F_WAIT;
    626  1.1.1.2  fvdl 		VOP_UNLOCK(vp);
    627  1.1.1.2  fvdl 		if (error = VOP_ADVLOCK(vp, (caddr_t)fp, F_SETLK, &lf, type)) {
    628  1.1.1.2  fvdl 			(void) vn_close(vp, fp->f_flag, fp->f_cred, p);
    629  1.1.1.2  fvdl 			ffree(fp);
    630  1.1.1.2  fvdl 			fdp->fd_ofiles[indx] = NULL;
    631  1.1.1.2  fvdl 			return (error);
    632  1.1.1.2  fvdl 		}
    633  1.1.1.2  fvdl 		VOP_LOCK(vp);
    634  1.1.1.2  fvdl 		fp->f_flag |= FHASLOCK;
    635  1.1.1.2  fvdl 	}
    636  1.1.1.2  fvdl 	VOP_UNLOCK(vp);
    637  1.1.1.2  fvdl 	*retval = indx;
    638  1.1.1.2  fvdl 	return (0);
    639  1.1.1.2  fvdl }
    640  1.1.1.2  fvdl 
    641  1.1.1.2  fvdl #ifdef COMPAT_43
    642  1.1.1.2  fvdl /*
    643  1.1.1.2  fvdl  * Create a file.
    644  1.1.1.2  fvdl  */
    645  1.1.1.2  fvdl struct ocreat_args {
    646  1.1.1.2  fvdl 	char	*path;
    647  1.1.1.2  fvdl 	int	mode;
    648  1.1.1.2  fvdl };
    649  1.1.1.2  fvdl ocreat(p, uap, retval)
    650  1.1.1.2  fvdl 	struct proc *p;
    651  1.1.1.2  fvdl 	register struct ocreat_args *uap;
    652  1.1.1.2  fvdl 	int *retval;
    653  1.1.1.2  fvdl {
    654  1.1.1.2  fvdl 	struct open_args openuap;
    655  1.1.1.2  fvdl 
    656  1.1.1.2  fvdl 	openuap.path = uap->path;
    657  1.1.1.2  fvdl 	openuap.mode = uap->mode;
    658  1.1.1.2  fvdl 	openuap.flags = O_WRONLY | O_CREAT | O_TRUNC;
    659  1.1.1.2  fvdl 	return (open(p, &openuap, retval));
    660  1.1.1.2  fvdl }
    661  1.1.1.2  fvdl #endif /* COMPAT_43 */
    662  1.1.1.2  fvdl 
    663  1.1.1.2  fvdl /*
    664  1.1.1.2  fvdl  * Create a special file.
    665  1.1.1.2  fvdl  */
    666  1.1.1.2  fvdl struct mknod_args {
    667  1.1.1.2  fvdl 	char	*path;
    668  1.1.1.2  fvdl 	int	mode;
    669  1.1.1.2  fvdl 	int	dev;
    670  1.1.1.2  fvdl };
    671  1.1.1.2  fvdl /* ARGSUSED */
    672  1.1.1.2  fvdl mknod(p, uap, retval)
    673  1.1.1.2  fvdl 	struct proc *p;
    674  1.1.1.2  fvdl 	register struct mknod_args *uap;
    675  1.1.1.2  fvdl 	int *retval;
    676  1.1.1.2  fvdl {
    677  1.1.1.2  fvdl 	register struct vnode *vp;
    678  1.1.1.2  fvdl 	struct vattr vattr;
    679  1.1.1.2  fvdl 	int error;
    680  1.1.1.2  fvdl 	struct nameidata nd;
    681  1.1.1.2  fvdl 
    682  1.1.1.2  fvdl 	if (error = suser(p->p_ucred, &p->p_acflag))
    683  1.1.1.2  fvdl 		return (error);
    684  1.1.1.2  fvdl 	NDINIT(&nd, CREATE, LOCKPARENT, UIO_USERSPACE, uap->path, p);
    685  1.1.1.2  fvdl 	if (error = namei(&nd))
    686  1.1.1.2  fvdl 		return (error);
    687  1.1.1.2  fvdl 	vp = nd.ni_vp;
    688  1.1.1.2  fvdl 	if (vp != NULL)
    689  1.1.1.2  fvdl 		error = EEXIST;
    690  1.1.1.2  fvdl 	else {
    691  1.1.1.2  fvdl 		VATTR_NULL(&vattr);
    692  1.1.1.2  fvdl 		vattr.va_mode = (uap->mode & ALLPERMS) &~ p->p_fd->fd_cmask;
    693  1.1.1.2  fvdl 		vattr.va_rdev = uap->dev;
    694  1.1.1.2  fvdl 
    695  1.1.1.2  fvdl 		switch (uap->mode & S_IFMT) {
    696  1.1.1.2  fvdl 		case S_IFMT:	/* used by badsect to flag bad sectors */
    697  1.1.1.2  fvdl 			vattr.va_type = VBAD;
    698  1.1.1.2  fvdl 			break;
    699  1.1.1.2  fvdl 		case S_IFCHR:
    700  1.1.1.2  fvdl 			vattr.va_type = VCHR;
    701  1.1.1.2  fvdl 			break;
    702  1.1.1.2  fvdl 		case S_IFBLK:
    703  1.1.1.2  fvdl 			vattr.va_type = VBLK;
    704  1.1.1.2  fvdl 			break;
    705  1.1.1.2  fvdl 		default:
    706  1.1.1.2  fvdl 			error = EINVAL;
    707  1.1.1.2  fvdl 			break;
    708  1.1.1.2  fvdl 		}
    709  1.1.1.2  fvdl 	}
    710  1.1.1.2  fvdl 	if (!error) {
    711  1.1.1.2  fvdl 		LEASE_CHECK(nd.ni_dvp, p, p->p_ucred, LEASE_WRITE);
    712  1.1.1.2  fvdl 		error = VOP_MKNOD(nd.ni_dvp, &nd.ni_vp, &nd.ni_cnd, &vattr);
    713  1.1.1.2  fvdl 	} else {
    714  1.1.1.2  fvdl 		VOP_ABORTOP(nd.ni_dvp, &nd.ni_cnd);
    715  1.1.1.2  fvdl 		if (nd.ni_dvp == vp)
    716  1.1.1.2  fvdl 			vrele(nd.ni_dvp);
    717  1.1.1.2  fvdl 		else
    718  1.1.1.2  fvdl 			vput(nd.ni_dvp);
    719  1.1.1.2  fvdl 		if (vp)
    720  1.1.1.2  fvdl 			vrele(vp);
    721  1.1.1.2  fvdl 	}
    722  1.1.1.2  fvdl 	return (error);
    723  1.1.1.2  fvdl }
    724  1.1.1.2  fvdl 
    725  1.1.1.2  fvdl /*
    726  1.1.1.2  fvdl  * Create named pipe.
    727  1.1.1.2  fvdl  */
    728  1.1.1.2  fvdl struct mkfifo_args {
    729  1.1.1.2  fvdl 	char	*path;
    730  1.1.1.2  fvdl 	int	mode;
    731  1.1.1.2  fvdl };
    732  1.1.1.2  fvdl /* ARGSUSED */
    733  1.1.1.2  fvdl mkfifo(p, uap, retval)
    734  1.1.1.2  fvdl 	struct proc *p;
    735  1.1.1.2  fvdl 	register struct mkfifo_args *uap;
    736  1.1.1.2  fvdl 	int *retval;
    737  1.1.1.2  fvdl {
    738  1.1.1.2  fvdl 	struct vattr vattr;
    739  1.1.1.2  fvdl 	int error;
    740  1.1.1.2  fvdl 	struct nameidata nd;
    741  1.1.1.2  fvdl 
    742  1.1.1.2  fvdl #ifndef FIFO
    743  1.1.1.2  fvdl 	return (EOPNOTSUPP);
    744  1.1.1.2  fvdl #else
    745  1.1.1.2  fvdl 	NDINIT(&nd, CREATE, LOCKPARENT, UIO_USERSPACE, uap->path, p);
    746  1.1.1.2  fvdl 	if (error = namei(&nd))
    747  1.1.1.2  fvdl 		return (error);
    748  1.1.1.2  fvdl 	if (nd.ni_vp != NULL) {
    749  1.1.1.2  fvdl 		VOP_ABORTOP(nd.ni_dvp, &nd.ni_cnd);
    750  1.1.1.2  fvdl 		if (nd.ni_dvp == nd.ni_vp)
    751  1.1.1.2  fvdl 			vrele(nd.ni_dvp);
    752  1.1.1.2  fvdl 		else
    753  1.1.1.2  fvdl 			vput(nd.ni_dvp);
    754  1.1.1.2  fvdl 		vrele(nd.ni_vp);
    755  1.1.1.2  fvdl 		return (EEXIST);
    756  1.1.1.2  fvdl 	}
    757  1.1.1.2  fvdl 	VATTR_NULL(&vattr);
    758  1.1.1.2  fvdl 	vattr.va_type = VFIFO;
    759  1.1.1.2  fvdl 	vattr.va_mode = (uap->mode & ALLPERMS) &~ p->p_fd->fd_cmask;
    760  1.1.1.2  fvdl 	LEASE_CHECK(nd.ni_dvp, p, p->p_ucred, LEASE_WRITE);
    761  1.1.1.2  fvdl 	return (VOP_MKNOD(nd.ni_dvp, &nd.ni_vp, &nd.ni_cnd, &vattr));
    762  1.1.1.2  fvdl #endif /* FIFO */
    763  1.1.1.2  fvdl }
    764  1.1.1.2  fvdl 
    765  1.1.1.2  fvdl /*
    766  1.1.1.2  fvdl  * Make a hard file link.
    767  1.1.1.2  fvdl  */
    768  1.1.1.2  fvdl struct link_args {
    769  1.1.1.2  fvdl 	char	*path;
    770  1.1.1.2  fvdl 	char	*link;
    771  1.1.1.2  fvdl };
    772  1.1.1.2  fvdl /* ARGSUSED */
    773  1.1.1.2  fvdl link(p, uap, retval)
    774  1.1.1.2  fvdl 	struct proc *p;
    775  1.1.1.2  fvdl 	register struct link_args *uap;
    776  1.1.1.2  fvdl 	int *retval;
    777  1.1.1.2  fvdl {
    778  1.1.1.2  fvdl 	register struct vnode *vp;
    779  1.1.1.2  fvdl 	struct nameidata nd;
    780  1.1.1.2  fvdl 	int error;
    781  1.1.1.2  fvdl 
    782  1.1.1.2  fvdl 	NDINIT(&nd, LOOKUP, FOLLOW, UIO_USERSPACE, uap->path, p);
    783  1.1.1.2  fvdl 	if (error = namei(&nd))
    784  1.1.1.2  fvdl 		return (error);
    785  1.1.1.2  fvdl 	vp = nd.ni_vp;
    786  1.1.1.2  fvdl 	if (vp->v_type != VDIR ||
    787  1.1.1.2  fvdl 	    (error = suser(p->p_ucred, &p->p_acflag)) == 0) {
    788  1.1.1.2  fvdl 		nd.ni_cnd.cn_nameiop = CREATE;
    789  1.1.1.2  fvdl 		nd.ni_cnd.cn_flags = LOCKPARENT;
    790  1.1.1.2  fvdl 		nd.ni_dirp = uap->link;
    791  1.1.1.2  fvdl 		if ((error = namei(&nd)) == 0) {
    792  1.1.1.2  fvdl 			if (nd.ni_vp != NULL)
    793  1.1.1.2  fvdl 				error = EEXIST;
    794  1.1.1.2  fvdl 			if (!error) {
    795  1.1.1.2  fvdl 				LEASE_CHECK(nd.ni_dvp,
    796  1.1.1.2  fvdl 				    p, p->p_ucred, LEASE_WRITE);
    797  1.1.1.2  fvdl 				LEASE_CHECK(vp,
    798  1.1.1.2  fvdl 				    p, p->p_ucred, LEASE_WRITE);
    799  1.1.1.2  fvdl 				error = VOP_LINK(nd.ni_dvp, vp, &nd.ni_cnd);
    800  1.1.1.2  fvdl 			} else {
    801  1.1.1.2  fvdl 				VOP_ABORTOP(nd.ni_dvp, &nd.ni_cnd);
    802  1.1.1.2  fvdl 				if (nd.ni_dvp == nd.ni_vp)
    803  1.1.1.2  fvdl 					vrele(nd.ni_dvp);
    804  1.1.1.2  fvdl 				else
    805  1.1.1.2  fvdl 					vput(nd.ni_dvp);
    806  1.1.1.2  fvdl 				if (nd.ni_vp)
    807  1.1.1.2  fvdl 					vrele(nd.ni_vp);
    808  1.1.1.2  fvdl 			}
    809  1.1.1.2  fvdl 		}
    810  1.1.1.2  fvdl 	}
    811  1.1.1.2  fvdl 	vrele(vp);
    812  1.1.1.2  fvdl 	return (error);
    813  1.1.1.2  fvdl }
    814  1.1.1.2  fvdl 
    815  1.1.1.2  fvdl /*
    816  1.1.1.2  fvdl  * Make a symbolic link.
    817  1.1.1.2  fvdl  */
    818  1.1.1.2  fvdl struct symlink_args {
    819  1.1.1.2  fvdl 	char	*path;
    820  1.1.1.2  fvdl 	char	*link;
    821  1.1.1.2  fvdl };
    822  1.1.1.2  fvdl /* ARGSUSED */
    823  1.1.1.2  fvdl symlink(p, uap, retval)
    824  1.1.1.2  fvdl 	struct proc *p;
    825  1.1.1.2  fvdl 	register struct symlink_args *uap;
    826  1.1.1.2  fvdl 	int *retval;
    827  1.1.1.2  fvdl {
    828  1.1.1.2  fvdl 	struct vattr vattr;
    829  1.1.1.2  fvdl 	char *path;
    830  1.1.1.2  fvdl 	int error;
    831  1.1.1.2  fvdl 	struct nameidata nd;
    832  1.1.1.2  fvdl 
    833  1.1.1.2  fvdl 	MALLOC(path, char *, MAXPATHLEN, M_NAMEI, M_WAITOK);
    834  1.1.1.2  fvdl 	if (error = copyinstr(uap->path, path, MAXPATHLEN, NULL))
    835  1.1.1.2  fvdl 		goto out;
    836  1.1.1.2  fvdl 	NDINIT(&nd, CREATE, LOCKPARENT, UIO_USERSPACE, uap->link, p);
    837  1.1.1.2  fvdl 	if (error = namei(&nd))
    838  1.1.1.2  fvdl 		goto out;
    839  1.1.1.2  fvdl 	if (nd.ni_vp) {
    840  1.1.1.2  fvdl 		VOP_ABORTOP(nd.ni_dvp, &nd.ni_cnd);
    841  1.1.1.2  fvdl 		if (nd.ni_dvp == nd.ni_vp)
    842  1.1.1.2  fvdl 			vrele(nd.ni_dvp);
    843  1.1.1.2  fvdl 		else
    844  1.1.1.2  fvdl 			vput(nd.ni_dvp);
    845  1.1.1.2  fvdl 		vrele(nd.ni_vp);
    846  1.1.1.2  fvdl 		error = EEXIST;
    847  1.1.1.2  fvdl 		goto out;
    848  1.1.1.2  fvdl 	}
    849  1.1.1.2  fvdl 	VATTR_NULL(&vattr);
    850  1.1.1.2  fvdl 	vattr.va_mode = ACCESSPERMS &~ p->p_fd->fd_cmask;
    851  1.1.1.2  fvdl 	LEASE_CHECK(nd.ni_dvp, p, p->p_ucred, LEASE_WRITE);
    852  1.1.1.2  fvdl 	error = VOP_SYMLINK(nd.ni_dvp, &nd.ni_vp, &nd.ni_cnd, &vattr, path);
    853  1.1.1.2  fvdl out:
    854  1.1.1.2  fvdl 	FREE(path, M_NAMEI);
    855  1.1.1.2  fvdl 	return (error);
    856  1.1.1.2  fvdl }
    857  1.1.1.2  fvdl 
    858  1.1.1.2  fvdl /*
    859  1.1.1.2  fvdl  * Delete a name from the filesystem.
    860  1.1.1.2  fvdl  */
    861  1.1.1.2  fvdl struct unlink_args {
    862  1.1.1.2  fvdl 	char	*path;
    863  1.1.1.2  fvdl };
    864  1.1.1.2  fvdl /* ARGSUSED */
    865  1.1.1.2  fvdl unlink(p, uap, retval)
    866  1.1.1.2  fvdl 	struct proc *p;
    867  1.1.1.2  fvdl 	struct unlink_args *uap;
    868  1.1.1.2  fvdl 	int *retval;
    869  1.1.1.2  fvdl {
    870  1.1.1.2  fvdl 	register struct vnode *vp;
    871  1.1.1.2  fvdl 	int error;
    872  1.1.1.2  fvdl 	struct nameidata nd;
    873  1.1.1.2  fvdl 
    874  1.1.1.2  fvdl 	NDINIT(&nd, DELETE, LOCKPARENT, UIO_USERSPACE, uap->path, p);
    875  1.1.1.2  fvdl 	if (error = namei(&nd))
    876  1.1.1.2  fvdl 		return (error);
    877  1.1.1.2  fvdl 	vp = nd.ni_vp;
    878  1.1.1.2  fvdl 	LEASE_CHECK(vp, p, p->p_ucred, LEASE_WRITE);
    879  1.1.1.2  fvdl 	VOP_LOCK(vp);
    880  1.1.1.2  fvdl 
    881  1.1.1.2  fvdl 	if (vp->v_type != VDIR ||
    882  1.1.1.2  fvdl 	    (error = suser(p->p_ucred, &p->p_acflag)) == 0) {
    883  1.1.1.2  fvdl 		/*
    884  1.1.1.2  fvdl 		 * The root of a mounted filesystem cannot be deleted.
    885  1.1.1.2  fvdl 		 */
    886  1.1.1.2  fvdl 		if (vp->v_flag & VROOT)
    887  1.1.1.2  fvdl 			error = EBUSY;
    888  1.1.1.2  fvdl 		else
    889  1.1.1.2  fvdl 			(void)vnode_pager_uncache(vp);
    890  1.1.1.2  fvdl 	}
    891  1.1.1.2  fvdl 
    892  1.1.1.2  fvdl 	if (!error) {
    893  1.1.1.2  fvdl 		LEASE_CHECK(nd.ni_dvp, p, p->p_ucred, LEASE_WRITE);
    894  1.1.1.2  fvdl 		error = VOP_REMOVE(nd.ni_dvp, nd.ni_vp, &nd.ni_cnd);
    895  1.1.1.2  fvdl 	} else {
    896  1.1.1.2  fvdl 		VOP_ABORTOP(nd.ni_dvp, &nd.ni_cnd);
    897  1.1.1.2  fvdl 		if (nd.ni_dvp == vp)
    898  1.1.1.2  fvdl 			vrele(nd.ni_dvp);
    899  1.1.1.2  fvdl 		else
    900  1.1.1.2  fvdl 			vput(nd.ni_dvp);
    901  1.1.1.2  fvdl 		vput(vp);
    902  1.1.1.2  fvdl 	}
    903  1.1.1.2  fvdl 	return (error);
    904  1.1.1.2  fvdl }
    905  1.1.1.2  fvdl 
    906  1.1.1.2  fvdl /*
    907  1.1.1.2  fvdl  * Reposition read/write file offset.
    908  1.1.1.2  fvdl  */
    909  1.1.1.2  fvdl struct lseek_args {
    910  1.1.1.2  fvdl 	int	fd;
    911  1.1.1.2  fvdl 	int	pad;
    912  1.1.1.2  fvdl 	off_t	offset;
    913  1.1.1.2  fvdl 	int	whence;
    914  1.1.1.2  fvdl };
    915  1.1.1.2  fvdl lseek(p, uap, retval)
    916  1.1.1.2  fvdl 	struct proc *p;
    917  1.1.1.2  fvdl 	register struct lseek_args *uap;
    918  1.1.1.2  fvdl 	int *retval;
    919  1.1.1.2  fvdl {
    920  1.1.1.2  fvdl 	struct ucred *cred = p->p_ucred;
    921  1.1.1.2  fvdl 	register struct filedesc *fdp = p->p_fd;
    922  1.1.1.2  fvdl 	register struct file *fp;
    923  1.1.1.2  fvdl 	struct vattr vattr;
    924  1.1.1.2  fvdl 	int error;
    925  1.1.1.2  fvdl 
    926  1.1.1.2  fvdl 	if ((u_int)uap->fd >= fdp->fd_nfiles ||
    927  1.1.1.2  fvdl 	    (fp = fdp->fd_ofiles[uap->fd]) == NULL)
    928  1.1.1.2  fvdl 		return (EBADF);
    929  1.1.1.2  fvdl 	if (fp->f_type != DTYPE_VNODE)
    930  1.1.1.2  fvdl 		return (ESPIPE);
    931  1.1.1.2  fvdl 	switch (uap->whence) {
    932  1.1.1.2  fvdl 	case L_INCR:
    933  1.1.1.2  fvdl 		fp->f_offset += uap->offset;
    934  1.1.1.2  fvdl 		break;
    935  1.1.1.2  fvdl 	case L_XTND:
    936  1.1.1.2  fvdl 		if (error =
    937  1.1.1.2  fvdl 		    VOP_GETATTR((struct vnode *)fp->f_data, &vattr, cred, p))
    938  1.1.1.2  fvdl 			return (error);
    939  1.1.1.2  fvdl 		fp->f_offset = uap->offset + vattr.va_size;
    940  1.1.1.2  fvdl 		break;
    941  1.1.1.2  fvdl 	case L_SET:
    942  1.1.1.2  fvdl 		fp->f_offset = uap->offset;
    943  1.1.1.2  fvdl 		break;
    944  1.1.1.2  fvdl 	default:
    945  1.1.1.2  fvdl 		return (EINVAL);
    946  1.1.1.2  fvdl 	}
    947  1.1.1.2  fvdl 	*(off_t *)retval = fp->f_offset;
    948  1.1.1.2  fvdl 	return (0);
    949  1.1.1.2  fvdl }
    950  1.1.1.2  fvdl 
    951  1.1.1.2  fvdl #if defined(COMPAT_43) || defined(COMPAT_SUNOS)
    952  1.1.1.2  fvdl /*
    953  1.1.1.2  fvdl  * Reposition read/write file offset.
    954  1.1.1.2  fvdl  */
    955  1.1.1.2  fvdl struct olseek_args {
    956  1.1.1.2  fvdl 	int	fd;
    957  1.1.1.2  fvdl 	long	offset;
    958  1.1.1.2  fvdl 	int	whence;
    959  1.1.1.2  fvdl };
    960  1.1.1.2  fvdl olseek(p, uap, retval)
    961  1.1.1.2  fvdl 	struct proc *p;
    962  1.1.1.2  fvdl 	register struct olseek_args *uap;
    963  1.1.1.2  fvdl 	int *retval;
    964  1.1.1.2  fvdl {
    965  1.1.1.2  fvdl 	struct lseek_args nuap;
    966  1.1.1.2  fvdl 	off_t qret;
    967  1.1.1.2  fvdl 	int error;
    968  1.1.1.2  fvdl 
    969  1.1.1.2  fvdl 	nuap.fd = uap->fd;
    970  1.1.1.2  fvdl 	nuap.offset = uap->offset;
    971  1.1.1.2  fvdl 	nuap.whence = uap->whence;
    972  1.1.1.2  fvdl 	error = lseek(p, &nuap, &qret);
    973  1.1.1.2  fvdl 	*(long *)retval = qret;
    974  1.1.1.2  fvdl 	return (error);
    975  1.1.1.2  fvdl }
    976  1.1.1.2  fvdl #endif /* COMPAT_43 */
    977  1.1.1.2  fvdl 
    978  1.1.1.2  fvdl /*
    979  1.1.1.2  fvdl  * Check access permissions.
    980  1.1.1.2  fvdl  */
    981  1.1.1.2  fvdl struct access_args {
    982  1.1.1.2  fvdl 	char	*path;
    983  1.1.1.2  fvdl 	int	flags;
    984  1.1.1.2  fvdl };
    985  1.1.1.2  fvdl access(p, uap, retval)
    986  1.1.1.2  fvdl 	struct proc *p;
    987  1.1.1.2  fvdl 	register struct access_args *uap;
    988  1.1.1.2  fvdl 	int *retval;
    989  1.1.1.2  fvdl {
    990  1.1.1.2  fvdl 	register struct ucred *cred = p->p_ucred;
    991  1.1.1.2  fvdl 	register struct vnode *vp;
    992  1.1.1.2  fvdl 	int error, flags, t_gid, t_uid;
    993  1.1.1.2  fvdl 	struct nameidata nd;
    994  1.1.1.2  fvdl 
    995  1.1.1.2  fvdl 	t_uid = cred->cr_uid;
    996  1.1.1.2  fvdl 	t_gid = cred->cr_groups[0];
    997  1.1.1.2  fvdl 	cred->cr_uid = p->p_cred->p_ruid;
    998  1.1.1.2  fvdl 	cred->cr_groups[0] = p->p_cred->p_rgid;
    999  1.1.1.2  fvdl 	NDINIT(&nd, LOOKUP, FOLLOW | LOCKLEAF, UIO_USERSPACE, uap->path, p);
   1000  1.1.1.2  fvdl 	if (error = namei(&nd))
   1001  1.1.1.2  fvdl 		goto out1;
   1002  1.1.1.2  fvdl 	vp = nd.ni_vp;
   1003  1.1.1.2  fvdl 
   1004  1.1.1.2  fvdl 	/* Flags == 0 means only check for existence. */
   1005  1.1.1.2  fvdl 	if (uap->flags) {
   1006  1.1.1.2  fvdl 		flags = 0;
   1007  1.1.1.2  fvdl 		if (uap->flags & R_OK)
   1008  1.1.1.2  fvdl 			flags |= VREAD;
   1009  1.1.1.2  fvdl 		if (uap->flags & W_OK)
   1010  1.1.1.2  fvdl 			flags |= VWRITE;
   1011  1.1.1.2  fvdl 		if (uap->flags & X_OK)
   1012  1.1.1.2  fvdl 			flags |= VEXEC;
   1013  1.1.1.2  fvdl 		if ((flags & VWRITE) == 0 || (error = vn_writechk(vp)) == 0)
   1014  1.1.1.2  fvdl 			error = VOP_ACCESS(vp, flags, cred, p);
   1015  1.1.1.2  fvdl 	}
   1016  1.1.1.2  fvdl 	vput(vp);
   1017  1.1.1.2  fvdl out1:
   1018  1.1.1.2  fvdl 	cred->cr_uid = t_uid;
   1019  1.1.1.2  fvdl 	cred->cr_groups[0] = t_gid;
   1020  1.1.1.2  fvdl 	return (error);
   1021  1.1.1.2  fvdl }
   1022  1.1.1.2  fvdl 
   1023  1.1.1.2  fvdl #if defined(COMPAT_43) || defined(COMPAT_SUNOS)
   1024  1.1.1.2  fvdl /*
   1025  1.1.1.2  fvdl  * Get file status; this version follows links.
   1026  1.1.1.2  fvdl  */
   1027  1.1.1.2  fvdl struct ostat_args {
   1028  1.1.1.2  fvdl 	char	*path;
   1029  1.1.1.2  fvdl 	struct ostat *ub;
   1030  1.1.1.2  fvdl };
   1031  1.1.1.2  fvdl /* ARGSUSED */
   1032  1.1.1.2  fvdl ostat(p, uap, retval)
   1033  1.1.1.2  fvdl 	struct proc *p;
   1034  1.1.1.2  fvdl 	register struct ostat_args *uap;
   1035  1.1.1.2  fvdl 	int *retval;
   1036  1.1.1.2  fvdl {
   1037  1.1.1.2  fvdl 	struct stat sb;
   1038  1.1.1.2  fvdl 	struct ostat osb;
   1039  1.1.1.2  fvdl 	int error;
   1040  1.1.1.2  fvdl 	struct nameidata nd;
   1041  1.1.1.2  fvdl 
   1042  1.1.1.2  fvdl 	NDINIT(&nd, LOOKUP, FOLLOW | LOCKLEAF, UIO_USERSPACE, uap->path, p);
   1043  1.1.1.2  fvdl 	if (error = namei(&nd))
   1044  1.1.1.2  fvdl 		return (error);
   1045  1.1.1.2  fvdl 	error = vn_stat(nd.ni_vp, &sb, p);
   1046  1.1.1.2  fvdl 	vput(nd.ni_vp);
   1047  1.1.1.2  fvdl 	if (error)
   1048  1.1.1.2  fvdl 		return (error);
   1049  1.1.1.2  fvdl 	cvtstat(&sb, &osb);
   1050  1.1.1.2  fvdl 	error = copyout((caddr_t)&osb, (caddr_t)uap->ub, sizeof (osb));
   1051  1.1.1.2  fvdl 	return (error);
   1052  1.1.1.2  fvdl }
   1053  1.1.1.2  fvdl 
   1054  1.1.1.2  fvdl /*
   1055  1.1.1.2  fvdl  * Get file status; this version does not follow links.
   1056  1.1.1.2  fvdl  */
   1057  1.1.1.2  fvdl struct olstat_args {
   1058  1.1.1.2  fvdl 	char	*path;
   1059  1.1.1.2  fvdl 	struct ostat *ub;
   1060  1.1.1.2  fvdl };
   1061  1.1.1.2  fvdl /* ARGSUSED */
   1062  1.1.1.2  fvdl olstat(p, uap, retval)
   1063  1.1.1.2  fvdl 	struct proc *p;
   1064  1.1.1.2  fvdl 	register struct olstat_args *uap;
   1065  1.1.1.2  fvdl 	int *retval;
   1066  1.1.1.2  fvdl {
   1067  1.1.1.2  fvdl 	struct stat sb;
   1068  1.1.1.2  fvdl 	struct ostat osb;
   1069  1.1.1.2  fvdl 	int error;
   1070  1.1.1.2  fvdl 	struct nameidata nd;
   1071  1.1.1.2  fvdl 
   1072  1.1.1.2  fvdl 	NDINIT(&nd, LOOKUP, NOFOLLOW | LOCKLEAF, UIO_USERSPACE, uap->path, p);
   1073  1.1.1.2  fvdl 	if (error = namei(&nd))
   1074  1.1.1.2  fvdl 		return (error);
   1075  1.1.1.2  fvdl 	error = vn_stat(nd.ni_vp, &sb, p);
   1076  1.1.1.2  fvdl 	vput(nd.ni_vp);
   1077  1.1.1.2  fvdl 	if (error)
   1078  1.1.1.2  fvdl 		return (error);
   1079  1.1.1.2  fvdl 	cvtstat(&sb, &osb);
   1080  1.1.1.2  fvdl 	error = copyout((caddr_t)&osb, (caddr_t)uap->ub, sizeof (osb));
   1081  1.1.1.2  fvdl 	return (error);
   1082  1.1.1.2  fvdl }
   1083  1.1.1.2  fvdl 
   1084  1.1.1.2  fvdl /*
   1085  1.1.1.2  fvdl  * Convert from an old to a new stat structure.
   1086  1.1.1.2  fvdl  */
   1087  1.1.1.2  fvdl cvtstat(st, ost)
   1088  1.1.1.2  fvdl 	struct stat *st;
   1089  1.1.1.2  fvdl 	struct ostat *ost;
   1090  1.1.1.2  fvdl {
   1091  1.1.1.2  fvdl 
   1092  1.1.1.2  fvdl 	ost->st_dev = st->st_dev;
   1093  1.1.1.2  fvdl 	ost->st_ino = st->st_ino;
   1094  1.1.1.2  fvdl 	ost->st_mode = st->st_mode;
   1095  1.1.1.2  fvdl 	ost->st_nlink = st->st_nlink;
   1096  1.1.1.2  fvdl 	ost->st_uid = st->st_uid;
   1097  1.1.1.2  fvdl 	ost->st_gid = st->st_gid;
   1098  1.1.1.2  fvdl 	ost->st_rdev = st->st_rdev;
   1099  1.1.1.2  fvdl 	if (st->st_size < (quad_t)1 << 32)
   1100  1.1.1.2  fvdl 		ost->st_size = st->st_size;
   1101  1.1.1.2  fvdl 	else
   1102  1.1.1.2  fvdl 		ost->st_size = -2;
   1103  1.1.1.2  fvdl 	ost->st_atime = st->st_atime;
   1104  1.1.1.2  fvdl 	ost->st_mtime = st->st_mtime;
   1105  1.1.1.2  fvdl 	ost->st_ctime = st->st_ctime;
   1106  1.1.1.2  fvdl 	ost->st_blksize = st->st_blksize;
   1107  1.1.1.2  fvdl 	ost->st_blocks = st->st_blocks;
   1108  1.1.1.2  fvdl 	ost->st_flags = st->st_flags;
   1109  1.1.1.2  fvdl 	ost->st_gen = st->st_gen;
   1110  1.1.1.2  fvdl }
   1111  1.1.1.2  fvdl #endif /* COMPAT_43 || COMPAT_SUNOS */
   1112  1.1.1.2  fvdl 
   1113  1.1.1.2  fvdl /*
   1114  1.1.1.2  fvdl  * Get file status; this version follows links.
   1115  1.1.1.2  fvdl  */
   1116  1.1.1.2  fvdl struct stat_args {
   1117  1.1.1.2  fvdl 	char	*path;
   1118  1.1.1.2  fvdl 	struct stat *ub;
   1119  1.1.1.2  fvdl };
   1120  1.1.1.2  fvdl /* ARGSUSED */
   1121  1.1.1.2  fvdl stat(p, uap, retval)
   1122  1.1.1.2  fvdl 	struct proc *p;
   1123  1.1.1.2  fvdl 	register struct stat_args *uap;
   1124  1.1.1.2  fvdl 	int *retval;
   1125  1.1.1.2  fvdl {
   1126  1.1.1.2  fvdl 	struct stat sb;
   1127  1.1.1.2  fvdl 	int error;
   1128  1.1.1.2  fvdl 	struct nameidata nd;
   1129  1.1.1.2  fvdl 
   1130  1.1.1.2  fvdl 	NDINIT(&nd, LOOKUP, FOLLOW | LOCKLEAF, UIO_USERSPACE, uap->path, p);
   1131  1.1.1.2  fvdl 	if (error = namei(&nd))
   1132  1.1.1.2  fvdl 		return (error);
   1133  1.1.1.2  fvdl 	error = vn_stat(nd.ni_vp, &sb, p);
   1134  1.1.1.2  fvdl 	vput(nd.ni_vp);
   1135  1.1.1.2  fvdl 	if (error)
   1136  1.1.1.2  fvdl 		return (error);
   1137  1.1.1.2  fvdl 	error = copyout((caddr_t)&sb, (caddr_t)uap->ub, sizeof (sb));
   1138  1.1.1.2  fvdl 	return (error);
   1139  1.1.1.2  fvdl }
   1140  1.1.1.2  fvdl 
   1141  1.1.1.2  fvdl /*
   1142  1.1.1.2  fvdl  * Get file status; this version does not follow links.
   1143  1.1.1.2  fvdl  */
   1144  1.1.1.2  fvdl struct lstat_args {
   1145  1.1.1.2  fvdl 	char	*path;
   1146  1.1.1.2  fvdl 	struct stat *ub;
   1147  1.1.1.2  fvdl };
   1148  1.1.1.2  fvdl /* ARGSUSED */
   1149  1.1.1.2  fvdl lstat(p, uap, retval)
   1150  1.1.1.2  fvdl 	struct proc *p;
   1151  1.1.1.2  fvdl 	register struct lstat_args *uap;
   1152  1.1.1.2  fvdl 	int *retval;
   1153  1.1.1.2  fvdl {
   1154  1.1.1.2  fvdl 	int error;
   1155  1.1.1.2  fvdl 	struct vnode *vp, *dvp;
   1156  1.1.1.2  fvdl 	struct stat sb, sb1;
   1157  1.1.1.2  fvdl 	struct nameidata nd;
   1158  1.1.1.2  fvdl 
   1159  1.1.1.2  fvdl 	NDINIT(&nd, LOOKUP, NOFOLLOW | LOCKLEAF | LOCKPARENT, UIO_USERSPACE,
   1160  1.1.1.2  fvdl 	    uap->path, p);
   1161  1.1.1.2  fvdl 	if (error = namei(&nd))
   1162  1.1.1.2  fvdl 		return (error);
   1163  1.1.1.2  fvdl 	/*
   1164  1.1.1.2  fvdl 	 * For symbolic links, always return the attributes of its
   1165  1.1.1.2  fvdl 	 * containing directory, except for mode, size, and links.
   1166  1.1.1.2  fvdl 	 */
   1167  1.1.1.2  fvdl 	vp = nd.ni_vp;
   1168  1.1.1.2  fvdl 	dvp = nd.ni_dvp;
   1169  1.1.1.2  fvdl 	if (vp->v_type != VLNK) {
   1170  1.1.1.2  fvdl 		if (dvp == vp)
   1171  1.1.1.2  fvdl 			vrele(dvp);
   1172  1.1.1.2  fvdl 		else
   1173  1.1.1.2  fvdl 			vput(dvp);
   1174  1.1.1.2  fvdl 		error = vn_stat(vp, &sb, p);
   1175  1.1.1.2  fvdl 		vput(vp);
   1176  1.1.1.2  fvdl 		if (error)
   1177  1.1.1.2  fvdl 			return (error);
   1178  1.1.1.2  fvdl 	} else {
   1179  1.1.1.2  fvdl 		error = vn_stat(dvp, &sb, p);
   1180  1.1.1.2  fvdl 		vput(dvp);
   1181  1.1.1.2  fvdl 		if (error) {
   1182  1.1.1.2  fvdl 			vput(vp);
   1183  1.1.1.2  fvdl 			return (error);
   1184  1.1.1.2  fvdl 		}
   1185  1.1.1.2  fvdl 		error = vn_stat(vp, &sb1, p);
   1186  1.1.1.2  fvdl 		vput(vp);
   1187  1.1.1.2  fvdl 		if (error)
   1188  1.1.1.2  fvdl 			return (error);
   1189  1.1.1.2  fvdl 		sb.st_mode &= ~S_IFDIR;
   1190  1.1.1.2  fvdl 		sb.st_mode |= S_IFLNK;
   1191  1.1.1.2  fvdl 		sb.st_nlink = sb1.st_nlink;
   1192  1.1.1.2  fvdl 		sb.st_size = sb1.st_size;
   1193  1.1.1.2  fvdl 		sb.st_blocks = sb1.st_blocks;
   1194  1.1.1.2  fvdl 	}
   1195  1.1.1.2  fvdl 	error = copyout((caddr_t)&sb, (caddr_t)uap->ub, sizeof (sb));
   1196  1.1.1.2  fvdl 	return (error);
   1197  1.1.1.2  fvdl }
   1198  1.1.1.2  fvdl 
   1199  1.1.1.2  fvdl /*
   1200  1.1.1.2  fvdl  * Get configurable pathname variables.
   1201  1.1.1.2  fvdl  */
   1202  1.1.1.2  fvdl struct pathconf_args {
   1203  1.1.1.2  fvdl 	char	*path;
   1204  1.1.1.2  fvdl 	int	name;
   1205  1.1.1.2  fvdl };
   1206  1.1.1.2  fvdl /* ARGSUSED */
   1207  1.1.1.2  fvdl pathconf(p, uap, retval)
   1208  1.1.1.2  fvdl 	struct proc *p;
   1209  1.1.1.2  fvdl 	register struct pathconf_args *uap;
   1210  1.1.1.2  fvdl 	int *retval;
   1211  1.1.1.2  fvdl {
   1212  1.1.1.2  fvdl 	int error;
   1213  1.1.1.2  fvdl 	struct nameidata nd;
   1214  1.1.1.2  fvdl 
   1215  1.1.1.2  fvdl 	NDINIT(&nd, LOOKUP, FOLLOW | LOCKLEAF, UIO_USERSPACE, uap->path, p);
   1216  1.1.1.2  fvdl 	if (error = namei(&nd))
   1217  1.1.1.2  fvdl 		return (error);
   1218  1.1.1.2  fvdl 	error = VOP_PATHCONF(nd.ni_vp, uap->name, retval);
   1219  1.1.1.2  fvdl 	vput(nd.ni_vp);
   1220  1.1.1.2  fvdl 	return (error);
   1221  1.1.1.2  fvdl }
   1222  1.1.1.2  fvdl 
   1223  1.1.1.2  fvdl /*
   1224  1.1.1.2  fvdl  * Return target name of a symbolic link.
   1225  1.1.1.2  fvdl  */
   1226  1.1.1.2  fvdl struct readlink_args {
   1227  1.1.1.2  fvdl 	char	*path;
   1228  1.1.1.2  fvdl 	char	*buf;
   1229  1.1.1.2  fvdl 	int	count;
   1230  1.1.1.2  fvdl };
   1231  1.1.1.2  fvdl /* ARGSUSED */
   1232  1.1.1.2  fvdl readlink(p, uap, retval)
   1233  1.1.1.2  fvdl 	struct proc *p;
   1234  1.1.1.2  fvdl 	register struct readlink_args *uap;
   1235  1.1.1.2  fvdl 	int *retval;
   1236  1.1.1.2  fvdl {
   1237  1.1.1.2  fvdl 	register struct vnode *vp;
   1238  1.1.1.2  fvdl 	struct iovec aiov;
   1239  1.1.1.2  fvdl 	struct uio auio;
   1240  1.1.1.2  fvdl 	int error;
   1241  1.1.1.2  fvdl 	struct nameidata nd;
   1242  1.1.1.2  fvdl 
   1243  1.1.1.2  fvdl 	NDINIT(&nd, LOOKUP, NOFOLLOW | LOCKLEAF, UIO_USERSPACE, uap->path, p);
   1244  1.1.1.2  fvdl 	if (error = namei(&nd))
   1245  1.1.1.2  fvdl 		return (error);
   1246  1.1.1.2  fvdl 	vp = nd.ni_vp;
   1247  1.1.1.2  fvdl 	if (vp->v_type != VLNK)
   1248  1.1.1.2  fvdl 		error = EINVAL;
   1249  1.1.1.2  fvdl 	else {
   1250  1.1.1.2  fvdl 		aiov.iov_base = uap->buf;
   1251  1.1.1.2  fvdl 		aiov.iov_len = uap->count;
   1252  1.1.1.2  fvdl 		auio.uio_iov = &aiov;
   1253  1.1.1.2  fvdl 		auio.uio_iovcnt = 1;
   1254  1.1.1.2  fvdl 		auio.uio_offset = 0;
   1255  1.1.1.2  fvdl 		auio.uio_rw = UIO_READ;
   1256  1.1.1.2  fvdl 		auio.uio_segflg = UIO_USERSPACE;
   1257  1.1.1.2  fvdl 		auio.uio_procp = p;
   1258  1.1.1.2  fvdl 		auio.uio_resid = uap->count;
   1259  1.1.1.2  fvdl 		error = VOP_READLINK(vp, &auio, p->p_ucred);
   1260  1.1.1.2  fvdl 	}
   1261  1.1.1.2  fvdl 	vput(vp);
   1262  1.1.1.2  fvdl 	*retval = uap->count - auio.uio_resid;
   1263  1.1.1.2  fvdl 	return (error);
   1264  1.1.1.2  fvdl }
   1265  1.1.1.2  fvdl 
   1266  1.1.1.2  fvdl /*
   1267  1.1.1.2  fvdl  * Change flags of a file given a path name.
   1268  1.1.1.2  fvdl  */
   1269  1.1.1.2  fvdl struct chflags_args {
   1270  1.1.1.2  fvdl 	char	*path;
   1271  1.1.1.2  fvdl 	int	flags;
   1272  1.1.1.2  fvdl };
   1273  1.1.1.2  fvdl /* ARGSUSED */
   1274  1.1.1.2  fvdl chflags(p, uap, retval)
   1275  1.1.1.2  fvdl 	struct proc *p;
   1276  1.1.1.2  fvdl 	register struct chflags_args *uap;
   1277  1.1.1.2  fvdl 	int *retval;
   1278  1.1.1.2  fvdl {
   1279  1.1.1.2  fvdl 	register struct vnode *vp;
   1280  1.1.1.2  fvdl 	struct vattr vattr;
   1281  1.1.1.2  fvdl 	int error;
   1282  1.1.1.2  fvdl 	struct nameidata nd;
   1283  1.1.1.2  fvdl 
   1284  1.1.1.2  fvdl 	NDINIT(&nd, LOOKUP, FOLLOW, UIO_USERSPACE, uap->path, p);
   1285  1.1.1.2  fvdl 	if (error = namei(&nd))
   1286  1.1.1.2  fvdl 		return (error);
   1287  1.1.1.2  fvdl 	vp = nd.ni_vp;
   1288  1.1.1.2  fvdl 	LEASE_CHECK(vp, p, p->p_ucred, LEASE_WRITE);
   1289  1.1.1.2  fvdl 	VOP_LOCK(vp);
   1290  1.1.1.2  fvdl 	if (vp->v_mount->mnt_flag & MNT_RDONLY)
   1291  1.1.1.2  fvdl 		error = EROFS;
   1292  1.1.1.2  fvdl 	else {
   1293  1.1.1.2  fvdl 		VATTR_NULL(&vattr);
   1294  1.1.1.2  fvdl 		vattr.va_flags = uap->flags;
   1295  1.1.1.2  fvdl 		error = VOP_SETATTR(vp, &vattr, p->p_ucred, p);
   1296  1.1.1.2  fvdl 	}
   1297  1.1.1.2  fvdl 	vput(vp);
   1298  1.1.1.2  fvdl 	return (error);
   1299  1.1.1.2  fvdl }
   1300  1.1.1.2  fvdl 
   1301  1.1.1.2  fvdl /*
   1302  1.1.1.2  fvdl  * Change flags of a file given a file descriptor.
   1303  1.1.1.2  fvdl  */
   1304  1.1.1.2  fvdl struct fchflags_args {
   1305  1.1.1.2  fvdl 	int	fd;
   1306  1.1.1.2  fvdl 	int	flags;
   1307  1.1.1.2  fvdl };
   1308  1.1.1.2  fvdl /* ARGSUSED */
   1309  1.1.1.2  fvdl fchflags(p, uap, retval)
   1310  1.1.1.2  fvdl 	struct proc *p;
   1311  1.1.1.2  fvdl 	register struct fchflags_args *uap;
   1312  1.1.1.2  fvdl 	int *retval;
   1313  1.1.1.2  fvdl {
   1314  1.1.1.2  fvdl 	struct vattr vattr;
   1315  1.1.1.2  fvdl 	struct vnode *vp;
   1316  1.1.1.2  fvdl 	struct file *fp;
   1317  1.1.1.2  fvdl 	int error;
   1318  1.1.1.2  fvdl 
   1319  1.1.1.2  fvdl 	if (error = getvnode(p->p_fd, uap->fd, &fp))
   1320  1.1.1.2  fvdl 		return (error);
   1321  1.1.1.2  fvdl 	vp = (struct vnode *)fp->f_data;
   1322  1.1.1.2  fvdl 	LEASE_CHECK(vp, p, p->p_ucred, LEASE_WRITE);
   1323  1.1.1.2  fvdl 	VOP_LOCK(vp);
   1324  1.1.1.2  fvdl 	if (vp->v_mount->mnt_flag & MNT_RDONLY)
   1325  1.1.1.2  fvdl 		error = EROFS;
   1326  1.1.1.2  fvdl 	else {
   1327  1.1.1.2  fvdl 		VATTR_NULL(&vattr);
   1328  1.1.1.2  fvdl 		vattr.va_flags = uap->flags;
   1329  1.1.1.2  fvdl 		error = VOP_SETATTR(vp, &vattr, p->p_ucred, p);
   1330  1.1.1.2  fvdl 	}
   1331  1.1.1.2  fvdl 	VOP_UNLOCK(vp);
   1332  1.1.1.2  fvdl 	return (error);
   1333  1.1.1.2  fvdl }
   1334  1.1.1.2  fvdl 
   1335  1.1.1.2  fvdl /*
   1336  1.1.1.2  fvdl  * Change mode of a file given path name.
   1337  1.1.1.2  fvdl  */
   1338  1.1.1.2  fvdl struct chmod_args {
   1339  1.1.1.2  fvdl 	char	*path;
   1340  1.1.1.2  fvdl 	int	mode;
   1341  1.1.1.2  fvdl };
   1342  1.1.1.2  fvdl /* ARGSUSED */
   1343  1.1.1.2  fvdl chmod(p, uap, retval)
   1344  1.1.1.2  fvdl 	struct proc *p;
   1345  1.1.1.2  fvdl 	register struct chmod_args *uap;
   1346  1.1.1.2  fvdl 	int *retval;
   1347  1.1.1.2  fvdl {
   1348  1.1.1.2  fvdl 	register struct vnode *vp;
   1349  1.1.1.2  fvdl 	struct vattr vattr;
   1350  1.1.1.2  fvdl 	int error;
   1351  1.1.1.2  fvdl 	struct nameidata nd;
   1352  1.1.1.2  fvdl 
   1353  1.1.1.2  fvdl 	NDINIT(&nd, LOOKUP, FOLLOW, UIO_USERSPACE, uap->path, p);
   1354  1.1.1.2  fvdl 	if (error = namei(&nd))
   1355  1.1.1.2  fvdl 		return (error);
   1356  1.1.1.2  fvdl 	vp = nd.ni_vp;
   1357  1.1.1.2  fvdl 	LEASE_CHECK(vp, p, p->p_ucred, LEASE_WRITE);
   1358  1.1.1.2  fvdl 	VOP_LOCK(vp);
   1359  1.1.1.2  fvdl 	if (vp->v_mount->mnt_flag & MNT_RDONLY)
   1360  1.1.1.2  fvdl 		error = EROFS;
   1361  1.1.1.2  fvdl 	else {
   1362  1.1.1.2  fvdl 		VATTR_NULL(&vattr);
   1363  1.1.1.2  fvdl 		vattr.va_mode = uap->mode & ALLPERMS;
   1364  1.1.1.2  fvdl 		error = VOP_SETATTR(vp, &vattr, p->p_ucred, p);
   1365  1.1.1.2  fvdl 	}
   1366  1.1.1.2  fvdl 	vput(vp);
   1367  1.1.1.2  fvdl 	return (error);
   1368  1.1.1.2  fvdl }
   1369  1.1.1.2  fvdl 
   1370  1.1.1.2  fvdl /*
   1371  1.1.1.2  fvdl  * Change mode of a file given a file descriptor.
   1372  1.1.1.2  fvdl  */
   1373  1.1.1.2  fvdl struct fchmod_args {
   1374  1.1.1.2  fvdl 	int	fd;
   1375  1.1.1.2  fvdl 	int	mode;
   1376  1.1.1.2  fvdl };
   1377  1.1.1.2  fvdl /* ARGSUSED */
   1378  1.1.1.2  fvdl fchmod(p, uap, retval)
   1379  1.1.1.2  fvdl 	struct proc *p;
   1380  1.1.1.2  fvdl 	register struct fchmod_args *uap;
   1381  1.1.1.2  fvdl 	int *retval;
   1382  1.1.1.2  fvdl {
   1383  1.1.1.2  fvdl 	struct vattr vattr;
   1384  1.1.1.2  fvdl 	struct vnode *vp;
   1385  1.1.1.2  fvdl 	struct file *fp;
   1386  1.1.1.2  fvdl 	int error;
   1387  1.1.1.2  fvdl 
   1388  1.1.1.2  fvdl 	if (error = getvnode(p->p_fd, uap->fd, &fp))
   1389  1.1.1.2  fvdl 		return (error);
   1390  1.1.1.2  fvdl 	vp = (struct vnode *)fp->f_data;
   1391  1.1.1.2  fvdl 	LEASE_CHECK(vp, p, p->p_ucred, LEASE_WRITE);
   1392  1.1.1.2  fvdl 	VOP_LOCK(vp);
   1393  1.1.1.2  fvdl 	if (vp->v_mount->mnt_flag & MNT_RDONLY)
   1394  1.1.1.2  fvdl 		error = EROFS;
   1395  1.1.1.2  fvdl 	else {
   1396  1.1.1.2  fvdl 		VATTR_NULL(&vattr);
   1397  1.1.1.2  fvdl 		vattr.va_mode = uap->mode & ALLPERMS;
   1398  1.1.1.2  fvdl 		error = VOP_SETATTR(vp, &vattr, p->p_ucred, p);
   1399  1.1.1.2  fvdl 	}
   1400  1.1.1.2  fvdl 	VOP_UNLOCK(vp);
   1401  1.1.1.2  fvdl 	return (error);
   1402  1.1.1.2  fvdl }
   1403  1.1.1.2  fvdl 
   1404  1.1.1.2  fvdl /*
   1405  1.1.1.2  fvdl  * Set ownership given a path name.
   1406  1.1.1.2  fvdl  */
   1407  1.1.1.2  fvdl struct chown_args {
   1408  1.1.1.2  fvdl 	char	*path;
   1409  1.1.1.2  fvdl 	int	uid;
   1410  1.1.1.2  fvdl 	int	gid;
   1411  1.1.1.2  fvdl };
   1412  1.1.1.2  fvdl /* ARGSUSED */
   1413  1.1.1.2  fvdl chown(p, uap, retval)
   1414  1.1.1.2  fvdl 	struct proc *p;
   1415  1.1.1.2  fvdl 	register struct chown_args *uap;
   1416  1.1.1.2  fvdl 	int *retval;
   1417  1.1.1.2  fvdl {
   1418  1.1.1.2  fvdl 	register struct vnode *vp;
   1419  1.1.1.2  fvdl 	struct vattr vattr;
   1420  1.1.1.2  fvdl 	int error;
   1421  1.1.1.2  fvdl 	struct nameidata nd;
   1422  1.1.1.2  fvdl 
   1423  1.1.1.2  fvdl 	NDINIT(&nd, LOOKUP, FOLLOW, UIO_USERSPACE, uap->path, p);
   1424  1.1.1.2  fvdl 	if (error = namei(&nd))
   1425  1.1.1.2  fvdl 		return (error);
   1426  1.1.1.2  fvdl 	vp = nd.ni_vp;
   1427  1.1.1.2  fvdl 	LEASE_CHECK(vp, p, p->p_ucred, LEASE_WRITE);
   1428  1.1.1.2  fvdl 	VOP_LOCK(vp);
   1429  1.1.1.2  fvdl 	if (vp->v_mount->mnt_flag & MNT_RDONLY)
   1430  1.1.1.2  fvdl 		error = EROFS;
   1431  1.1.1.2  fvdl 	else {
   1432  1.1.1.2  fvdl 		VATTR_NULL(&vattr);
   1433  1.1.1.2  fvdl 		vattr.va_uid = uap->uid;
   1434  1.1.1.2  fvdl 		vattr.va_gid = uap->gid;
   1435  1.1.1.2  fvdl 		error = VOP_SETATTR(vp, &vattr, p->p_ucred, p);
   1436  1.1.1.2  fvdl 	}
   1437  1.1.1.2  fvdl 	vput(vp);
   1438  1.1.1.2  fvdl 	return (error);
   1439  1.1.1.2  fvdl }
   1440  1.1.1.2  fvdl 
   1441  1.1.1.2  fvdl /*
   1442  1.1.1.2  fvdl  * Set ownership given a file descriptor.
   1443  1.1.1.2  fvdl  */
   1444  1.1.1.2  fvdl struct fchown_args {
   1445  1.1.1.2  fvdl 	int	fd;
   1446  1.1.1.2  fvdl 	int	uid;
   1447  1.1.1.2  fvdl 	int	gid;
   1448  1.1.1.2  fvdl };
   1449  1.1.1.2  fvdl /* ARGSUSED */
   1450  1.1.1.2  fvdl fchown(p, uap, retval)
   1451  1.1.1.2  fvdl 	struct proc *p;
   1452  1.1.1.2  fvdl 	register struct fchown_args *uap;
   1453  1.1.1.2  fvdl 	int *retval;
   1454  1.1.1.2  fvdl {
   1455  1.1.1.2  fvdl 	struct vattr vattr;
   1456  1.1.1.2  fvdl 	struct vnode *vp;
   1457  1.1.1.2  fvdl 	struct file *fp;
   1458  1.1.1.2  fvdl 	int error;
   1459  1.1.1.2  fvdl 
   1460  1.1.1.2  fvdl 	if (error = getvnode(p->p_fd, uap->fd, &fp))
   1461  1.1.1.2  fvdl 		return (error);
   1462  1.1.1.2  fvdl 	vp = (struct vnode *)fp->f_data;
   1463  1.1.1.2  fvdl 	LEASE_CHECK(vp, p, p->p_ucred, LEASE_WRITE);
   1464  1.1.1.2  fvdl 	VOP_LOCK(vp);
   1465  1.1.1.2  fvdl 	if (vp->v_mount->mnt_flag & MNT_RDONLY)
   1466  1.1.1.2  fvdl 		error = EROFS;
   1467  1.1.1.2  fvdl 	else {
   1468  1.1.1.2  fvdl 		VATTR_NULL(&vattr);
   1469  1.1.1.2  fvdl 		vattr.va_uid = uap->uid;
   1470  1.1.1.2  fvdl 		vattr.va_gid = uap->gid;
   1471  1.1.1.2  fvdl 		error = VOP_SETATTR(vp, &vattr, p->p_ucred, p);
   1472  1.1.1.2  fvdl 	}
   1473  1.1.1.2  fvdl 	VOP_UNLOCK(vp);
   1474  1.1.1.2  fvdl 	return (error);
   1475  1.1.1.2  fvdl }
   1476  1.1.1.2  fvdl 
   1477  1.1.1.2  fvdl /*
   1478  1.1.1.2  fvdl  * Set the access and modification times of a file.
   1479  1.1.1.2  fvdl  */
   1480  1.1.1.2  fvdl struct utimes_args {
   1481  1.1.1.2  fvdl 	char	*path;
   1482  1.1.1.2  fvdl 	struct	timeval *tptr;
   1483  1.1.1.2  fvdl };
   1484  1.1.1.2  fvdl /* ARGSUSED */
   1485  1.1.1.2  fvdl utimes(p, uap, retval)
   1486  1.1.1.2  fvdl 	struct proc *p;
   1487  1.1.1.2  fvdl 	register struct utimes_args *uap;
   1488  1.1.1.2  fvdl 	int *retval;
   1489  1.1.1.2  fvdl {
   1490  1.1.1.2  fvdl 	register struct vnode *vp;
   1491  1.1.1.2  fvdl 	struct timeval tv[2];
   1492  1.1.1.2  fvdl 	struct vattr vattr;
   1493  1.1.1.2  fvdl 	int error;
   1494  1.1.1.2  fvdl 	struct nameidata nd;
   1495  1.1.1.2  fvdl 
   1496  1.1.1.2  fvdl 	VATTR_NULL(&vattr);
   1497  1.1.1.2  fvdl 	if (uap->tptr == NULL) {
   1498  1.1.1.2  fvdl 		microtime(&tv[0]);
   1499  1.1.1.2  fvdl 		tv[1] = tv[0];
   1500  1.1.1.2  fvdl 		vattr.va_vaflags |= VA_UTIMES_NULL;
   1501  1.1.1.2  fvdl 	} else if (error = copyin((caddr_t)uap->tptr, (caddr_t)tv, sizeof (tv)))
   1502  1.1.1.2  fvdl   		return (error);
   1503  1.1.1.2  fvdl 	NDINIT(&nd, LOOKUP, FOLLOW, UIO_USERSPACE, uap->path, p);
   1504  1.1.1.2  fvdl 	if (error = namei(&nd))
   1505  1.1.1.2  fvdl 		return (error);
   1506  1.1.1.2  fvdl 	vp = nd.ni_vp;
   1507  1.1.1.2  fvdl 	LEASE_CHECK(vp, p, p->p_ucred, LEASE_WRITE);
   1508  1.1.1.2  fvdl 	VOP_LOCK(vp);
   1509  1.1.1.2  fvdl 	if (vp->v_mount->mnt_flag & MNT_RDONLY)
   1510  1.1.1.2  fvdl 		error = EROFS;
   1511  1.1.1.2  fvdl 	else {
   1512  1.1.1.2  fvdl 		vattr.va_atime.ts_sec = tv[0].tv_sec;
   1513  1.1.1.2  fvdl 		vattr.va_atime.ts_nsec = tv[0].tv_usec * 1000;
   1514  1.1.1.2  fvdl 		vattr.va_mtime.ts_sec = tv[1].tv_sec;
   1515  1.1.1.2  fvdl 		vattr.va_mtime.ts_nsec = tv[1].tv_usec * 1000;
   1516  1.1.1.2  fvdl 		error = VOP_SETATTR(vp, &vattr, p->p_ucred, p);
   1517  1.1.1.2  fvdl 	}
   1518  1.1.1.2  fvdl 	vput(vp);
   1519  1.1.1.2  fvdl 	return (error);
   1520  1.1.1.2  fvdl }
   1521  1.1.1.2  fvdl 
   1522  1.1.1.2  fvdl /*
   1523  1.1.1.2  fvdl  * Truncate a file given its path name.
   1524  1.1.1.2  fvdl  */
   1525  1.1.1.2  fvdl struct truncate_args {
   1526  1.1.1.2  fvdl 	char	*path;
   1527  1.1.1.2  fvdl 	int	pad;
   1528  1.1.1.2  fvdl 	off_t	length;
   1529  1.1.1.2  fvdl };
   1530  1.1.1.2  fvdl /* ARGSUSED */
   1531  1.1.1.2  fvdl truncate(p, uap, retval)
   1532  1.1.1.2  fvdl 	struct proc *p;
   1533  1.1.1.2  fvdl 	register struct truncate_args *uap;
   1534  1.1.1.2  fvdl 	int *retval;
   1535  1.1.1.2  fvdl {
   1536  1.1.1.2  fvdl 	register struct vnode *vp;
   1537  1.1.1.2  fvdl 	struct vattr vattr;
   1538  1.1.1.2  fvdl 	int error;
   1539  1.1.1.2  fvdl 	struct nameidata nd;
   1540  1.1.1.2  fvdl 
   1541  1.1.1.2  fvdl 	NDINIT(&nd, LOOKUP, FOLLOW, UIO_USERSPACE, uap->path, p);
   1542  1.1.1.2  fvdl 	if (error = namei(&nd))
   1543  1.1.1.2  fvdl 		return (error);
   1544  1.1.1.2  fvdl 	vp = nd.ni_vp;
   1545  1.1.1.2  fvdl 	LEASE_CHECK(vp, p, p->p_ucred, LEASE_WRITE);
   1546  1.1.1.2  fvdl 	VOP_LOCK(vp);
   1547  1.1.1.2  fvdl 	if (vp->v_type == VDIR)
   1548  1.1.1.2  fvdl 		error = EISDIR;
   1549  1.1.1.2  fvdl 	else if ((error = vn_writechk(vp)) == 0 &&
   1550  1.1.1.2  fvdl 	    (error = VOP_ACCESS(vp, VWRITE, p->p_ucred, p)) == 0) {
   1551  1.1.1.2  fvdl 		VATTR_NULL(&vattr);
   1552  1.1.1.2  fvdl 		vattr.va_size = uap->length;
   1553  1.1.1.2  fvdl 		error = VOP_SETATTR(vp, &vattr, p->p_ucred, p);
   1554  1.1.1.2  fvdl 	}
   1555  1.1.1.2  fvdl 	vput(vp);
   1556  1.1.1.2  fvdl 	return (error);
   1557  1.1.1.2  fvdl }
   1558  1.1.1.2  fvdl 
   1559  1.1.1.2  fvdl /*
   1560  1.1.1.2  fvdl  * Truncate a file given a file descriptor.
   1561  1.1.1.2  fvdl  */
   1562  1.1.1.2  fvdl struct ftruncate_args {
   1563  1.1.1.2  fvdl 	int	fd;
   1564  1.1.1.2  fvdl 	int	pad;
   1565  1.1.1.2  fvdl 	off_t	length;
   1566  1.1.1.2  fvdl };
   1567  1.1.1.2  fvdl /* ARGSUSED */
   1568  1.1.1.2  fvdl ftruncate(p, uap, retval)
   1569  1.1.1.2  fvdl 	struct proc *p;
   1570  1.1.1.2  fvdl 	register struct ftruncate_args *uap;
   1571  1.1.1.2  fvdl 	int *retval;
   1572  1.1.1.2  fvdl {
   1573  1.1.1.2  fvdl 	struct vattr vattr;
   1574  1.1.1.2  fvdl 	struct vnode *vp;
   1575  1.1.1.2  fvdl 	struct file *fp;
   1576  1.1.1.2  fvdl 	int error;
   1577  1.1.1.2  fvdl 
   1578  1.1.1.2  fvdl 	if (error = getvnode(p->p_fd, uap->fd, &fp))
   1579  1.1.1.2  fvdl 		return (error);
   1580  1.1.1.2  fvdl 	if ((fp->f_flag & FWRITE) == 0)
   1581  1.1.1.2  fvdl 		return (EINVAL);
   1582  1.1.1.2  fvdl 	vp = (struct vnode *)fp->f_data;
   1583  1.1.1.2  fvdl 	LEASE_CHECK(vp, p, p->p_ucred, LEASE_WRITE);
   1584  1.1.1.2  fvdl 	VOP_LOCK(vp);
   1585  1.1.1.2  fvdl 	if (vp->v_type == VDIR)
   1586  1.1.1.2  fvdl 		error = EISDIR;
   1587  1.1.1.2  fvdl 	else if ((error = vn_writechk(vp)) == 0) {
   1588  1.1.1.2  fvdl 		VATTR_NULL(&vattr);
   1589  1.1.1.2  fvdl 		vattr.va_size = uap->length;
   1590  1.1.1.2  fvdl 		error = VOP_SETATTR(vp, &vattr, fp->f_cred, p);
   1591  1.1.1.2  fvdl 	}
   1592  1.1.1.2  fvdl 	VOP_UNLOCK(vp);
   1593  1.1.1.2  fvdl 	return (error);
   1594  1.1.1.2  fvdl }
   1595  1.1.1.2  fvdl 
   1596  1.1.1.2  fvdl #if defined(COMPAT_43) || defined(COMPAT_SUNOS)
   1597  1.1.1.2  fvdl /*
   1598  1.1.1.2  fvdl  * Truncate a file given its path name.
   1599  1.1.1.2  fvdl  */
   1600  1.1.1.2  fvdl struct otruncate_args {
   1601  1.1.1.2  fvdl 	char	*path;
   1602  1.1.1.2  fvdl 	long	length;
   1603  1.1.1.2  fvdl };
   1604  1.1.1.2  fvdl /* ARGSUSED */
   1605  1.1.1.2  fvdl otruncate(p, uap, retval)
   1606  1.1.1.2  fvdl 	struct proc *p;
   1607  1.1.1.2  fvdl 	register struct otruncate_args *uap;
   1608  1.1.1.2  fvdl 	int *retval;
   1609  1.1.1.2  fvdl {
   1610  1.1.1.2  fvdl 	struct truncate_args nuap;
   1611  1.1.1.2  fvdl 
   1612  1.1.1.2  fvdl 	nuap.path = uap->path;
   1613  1.1.1.2  fvdl 	nuap.length = uap->length;
   1614  1.1.1.2  fvdl 	return (truncate(p, &nuap, retval));
   1615  1.1.1.2  fvdl }
   1616  1.1.1.2  fvdl 
   1617  1.1.1.2  fvdl /*
   1618  1.1.1.2  fvdl  * Truncate a file given a file descriptor.
   1619  1.1.1.2  fvdl  */
   1620  1.1.1.2  fvdl struct oftruncate_args {
   1621  1.1.1.2  fvdl 	int	fd;
   1622  1.1.1.2  fvdl 	long	length;
   1623  1.1.1.2  fvdl };
   1624  1.1.1.2  fvdl /* ARGSUSED */
   1625  1.1.1.2  fvdl oftruncate(p, uap, retval)
   1626  1.1.1.2  fvdl 	struct proc *p;
   1627  1.1.1.2  fvdl 	register struct oftruncate_args *uap;
   1628  1.1.1.2  fvdl 	int *retval;
   1629  1.1.1.2  fvdl {
   1630  1.1.1.2  fvdl 	struct ftruncate_args nuap;
   1631  1.1.1.2  fvdl 
   1632  1.1.1.2  fvdl 	nuap.fd = uap->fd;
   1633  1.1.1.2  fvdl 	nuap.length = uap->length;
   1634  1.1.1.2  fvdl 	return (ftruncate(p, &nuap, retval));
   1635  1.1.1.2  fvdl }
   1636  1.1.1.2  fvdl #endif /* COMPAT_43 || COMPAT_SUNOS */
   1637  1.1.1.2  fvdl 
   1638  1.1.1.2  fvdl /*
   1639  1.1.1.2  fvdl  * Sync an open file.
   1640  1.1.1.2  fvdl  */
   1641  1.1.1.2  fvdl struct fsync_args {
   1642  1.1.1.2  fvdl 	int	fd;
   1643  1.1.1.2  fvdl };
   1644  1.1.1.2  fvdl /* ARGSUSED */
   1645  1.1.1.2  fvdl fsync(p, uap, retval)
   1646  1.1.1.2  fvdl 	struct proc *p;
   1647  1.1.1.2  fvdl 	struct fsync_args *uap;
   1648  1.1.1.2  fvdl 	int *retval;
   1649  1.1.1.2  fvdl {
   1650  1.1.1.2  fvdl 	register struct vnode *vp;
   1651  1.1.1.2  fvdl 	struct file *fp;
   1652  1.1.1.2  fvdl 	int error;
   1653  1.1.1.2  fvdl 
   1654  1.1.1.2  fvdl 	if (error = getvnode(p->p_fd, uap->fd, &fp))
   1655  1.1.1.2  fvdl 		return (error);
   1656  1.1.1.2  fvdl 	vp = (struct vnode *)fp->f_data;
   1657  1.1.1.2  fvdl 	VOP_LOCK(vp);
   1658  1.1.1.2  fvdl 	error = VOP_FSYNC(vp, fp->f_cred, MNT_WAIT, p);
   1659  1.1.1.2  fvdl 	VOP_UNLOCK(vp);
   1660  1.1.1.2  fvdl 	return (error);
   1661  1.1.1.2  fvdl }
   1662  1.1.1.2  fvdl 
   1663  1.1.1.2  fvdl /*
   1664  1.1.1.2  fvdl  * Rename files.  Source and destination must either both be directories,
   1665  1.1.1.2  fvdl  * or both not be directories.  If target is a directory, it must be empty.
   1666  1.1.1.2  fvdl  */
   1667  1.1.1.2  fvdl struct rename_args {
   1668  1.1.1.2  fvdl 	char	*from;
   1669  1.1.1.2  fvdl 	char	*to;
   1670  1.1.1.2  fvdl };
   1671  1.1.1.2  fvdl /* ARGSUSED */
   1672  1.1.1.2  fvdl rename(p, uap, retval)
   1673  1.1.1.2  fvdl 	struct proc *p;
   1674  1.1.1.2  fvdl 	register struct rename_args *uap;
   1675  1.1.1.2  fvdl 	int *retval;
   1676  1.1.1.2  fvdl {
   1677  1.1.1.2  fvdl 	register struct vnode *tvp, *fvp, *tdvp;
   1678  1.1.1.2  fvdl 	struct nameidata fromnd, tond;
   1679  1.1.1.2  fvdl 	int error;
   1680  1.1.1.2  fvdl 
   1681  1.1.1.2  fvdl 	NDINIT(&fromnd, DELETE, WANTPARENT | SAVESTART, UIO_USERSPACE,
   1682  1.1.1.2  fvdl 		uap->from, p);
   1683  1.1.1.2  fvdl 	if (error = namei(&fromnd))
   1684  1.1.1.2  fvdl 		return (error);
   1685  1.1.1.2  fvdl 	fvp = fromnd.ni_vp;
   1686  1.1.1.2  fvdl 	NDINIT(&tond, RENAME, LOCKPARENT | LOCKLEAF | NOCACHE | SAVESTART,
   1687  1.1.1.2  fvdl 		UIO_USERSPACE, uap->to, p);
   1688  1.1.1.2  fvdl 	if (error = namei(&tond)) {
   1689  1.1.1.2  fvdl 		VOP_ABORTOP(fromnd.ni_dvp, &fromnd.ni_cnd);
   1690  1.1.1.2  fvdl 		vrele(fromnd.ni_dvp);
   1691  1.1.1.2  fvdl 		vrele(fvp);
   1692  1.1.1.2  fvdl 		goto out1;
   1693  1.1.1.2  fvdl 	}
   1694  1.1.1.2  fvdl 	tdvp = tond.ni_dvp;
   1695  1.1.1.2  fvdl 	tvp = tond.ni_vp;
   1696  1.1.1.2  fvdl 	if (tvp != NULL) {
   1697  1.1.1.2  fvdl 		if (fvp->v_type == VDIR && tvp->v_type != VDIR) {
   1698  1.1.1.2  fvdl 			error = ENOTDIR;
   1699  1.1.1.2  fvdl 			goto out;
   1700  1.1.1.2  fvdl 		} else if (fvp->v_type != VDIR && tvp->v_type == VDIR) {
   1701  1.1.1.2  fvdl 			error = EISDIR;
   1702  1.1.1.2  fvdl 			goto out;
   1703  1.1.1.2  fvdl 		}
   1704  1.1.1.2  fvdl 	}
   1705  1.1.1.2  fvdl 	if (fvp == tdvp)
   1706  1.1.1.2  fvdl 		error = EINVAL;
   1707  1.1.1.2  fvdl 	/*
   1708  1.1.1.2  fvdl 	 * If source is the same as the destination (that is the
   1709  1.1.1.2  fvdl 	 * same inode number with the same name in the same directory),
   1710  1.1.1.2  fvdl 	 * then there is nothing to do.
   1711  1.1.1.2  fvdl 	 */
   1712  1.1.1.2  fvdl 	if (fvp == tvp && fromnd.ni_dvp == tdvp &&
   1713  1.1.1.2  fvdl 	    fromnd.ni_cnd.cn_namelen == tond.ni_cnd.cn_namelen &&
   1714  1.1.1.2  fvdl 	    !bcmp(fromnd.ni_cnd.cn_nameptr, tond.ni_cnd.cn_nameptr,
   1715  1.1.1.2  fvdl 	      fromnd.ni_cnd.cn_namelen))
   1716  1.1.1.2  fvdl 		error = -1;
   1717  1.1.1.2  fvdl out:
   1718  1.1.1.2  fvdl 	if (!error) {
   1719  1.1.1.2  fvdl 		LEASE_CHECK(tdvp, p, p->p_ucred, LEASE_WRITE);
   1720  1.1.1.2  fvdl 		if (fromnd.ni_dvp != tdvp)
   1721  1.1.1.2  fvdl 			LEASE_CHECK(fromnd.ni_dvp, p, p->p_ucred, LEASE_WRITE);
   1722  1.1.1.2  fvdl 		if (tvp)
   1723  1.1.1.2  fvdl 			LEASE_CHECK(tvp, p, p->p_ucred, LEASE_WRITE);
   1724  1.1.1.2  fvdl 		error = VOP_RENAME(fromnd.ni_dvp, fromnd.ni_vp, &fromnd.ni_cnd,
   1725  1.1.1.2  fvdl 				   tond.ni_dvp, tond.ni_vp, &tond.ni_cnd);
   1726  1.1.1.2  fvdl 	} else {
   1727  1.1.1.2  fvdl 		VOP_ABORTOP(tond.ni_dvp, &tond.ni_cnd);
   1728  1.1.1.2  fvdl 		if (tdvp == tvp)
   1729  1.1.1.2  fvdl 			vrele(tdvp);
   1730  1.1.1.2  fvdl 		else
   1731  1.1.1.2  fvdl 			vput(tdvp);
   1732  1.1.1.2  fvdl 		if (tvp)
   1733  1.1.1.2  fvdl 			vput(tvp);
   1734  1.1.1.2  fvdl 		VOP_ABORTOP(fromnd.ni_dvp, &fromnd.ni_cnd);
   1735  1.1.1.2  fvdl 		vrele(fromnd.ni_dvp);
   1736  1.1.1.2  fvdl 		vrele(fvp);
   1737  1.1.1.2  fvdl 	}
   1738  1.1.1.2  fvdl 	vrele(tond.ni_startdir);
   1739  1.1.1.2  fvdl 	FREE(tond.ni_cnd.cn_pnbuf, M_NAMEI);
   1740  1.1.1.2  fvdl out1:
   1741  1.1.1.2  fvdl 	if (fromnd.ni_startdir)
   1742  1.1.1.2  fvdl 		vrele(fromnd.ni_startdir);
   1743  1.1.1.2  fvdl 	FREE(fromnd.ni_cnd.cn_pnbuf, M_NAMEI);
   1744  1.1.1.2  fvdl 	if (error == -1)
   1745  1.1.1.2  fvdl 		return (0);
   1746  1.1.1.2  fvdl 	return (error);
   1747  1.1.1.2  fvdl }
   1748  1.1.1.2  fvdl 
   1749  1.1.1.2  fvdl /*
   1750  1.1.1.2  fvdl  * Make a directory file.
   1751  1.1.1.2  fvdl  */
   1752  1.1.1.2  fvdl struct mkdir_args {
   1753  1.1.1.2  fvdl 	char	*path;
   1754  1.1.1.2  fvdl 	int	mode;
   1755  1.1.1.2  fvdl };
   1756  1.1.1.2  fvdl /* ARGSUSED */
   1757  1.1.1.2  fvdl mkdir(p, uap, retval)
   1758  1.1.1.2  fvdl 	struct proc *p;
   1759  1.1.1.2  fvdl 	register struct mkdir_args *uap;
   1760  1.1.1.2  fvdl 	int *retval;
   1761  1.1.1.2  fvdl {
   1762  1.1.1.2  fvdl 	register struct vnode *vp;
   1763  1.1.1.2  fvdl 	struct vattr vattr;
   1764  1.1.1.2  fvdl 	int error;
   1765  1.1.1.2  fvdl 	struct nameidata nd;
   1766  1.1.1.2  fvdl 
   1767  1.1.1.2  fvdl 	NDINIT(&nd, CREATE, LOCKPARENT, UIO_USERSPACE, uap->path, p);
   1768  1.1.1.2  fvdl 	if (error = namei(&nd))
   1769  1.1.1.2  fvdl 		return (error);
   1770  1.1.1.2  fvdl 	vp = nd.ni_vp;
   1771  1.1.1.2  fvdl 	if (vp != NULL) {
   1772  1.1.1.2  fvdl 		VOP_ABORTOP(nd.ni_dvp, &nd.ni_cnd);
   1773  1.1.1.2  fvdl 		if (nd.ni_dvp == vp)
   1774  1.1.1.2  fvdl 			vrele(nd.ni_dvp);
   1775  1.1.1.2  fvdl 		else
   1776  1.1.1.2  fvdl 			vput(nd.ni_dvp);
   1777  1.1.1.2  fvdl 		vrele(vp);
   1778  1.1.1.2  fvdl 		return (EEXIST);
   1779  1.1.1.2  fvdl 	}
   1780  1.1.1.2  fvdl 	VATTR_NULL(&vattr);
   1781  1.1.1.2  fvdl 	vattr.va_type = VDIR;
   1782  1.1.1.2  fvdl 	vattr.va_mode = (uap->mode & ACCESSPERMS) &~ p->p_fd->fd_cmask;
   1783  1.1.1.2  fvdl 	LEASE_CHECK(nd.ni_dvp, p, p->p_ucred, LEASE_WRITE);
   1784  1.1.1.2  fvdl 	error = VOP_MKDIR(nd.ni_dvp, &nd.ni_vp, &nd.ni_cnd, &vattr);
   1785  1.1.1.2  fvdl 	if (!error)
   1786  1.1.1.2  fvdl 		vput(nd.ni_vp);
   1787  1.1.1.2  fvdl 	return (error);
   1788  1.1.1.2  fvdl }
   1789  1.1.1.2  fvdl 
   1790  1.1.1.2  fvdl /*
   1791  1.1.1.2  fvdl  * Remove a directory file.
   1792  1.1.1.2  fvdl  */
   1793  1.1.1.2  fvdl struct rmdir_args {
   1794  1.1.1.2  fvdl 	char	*path;
   1795  1.1.1.2  fvdl };
   1796  1.1.1.2  fvdl /* ARGSUSED */
   1797  1.1.1.2  fvdl rmdir(p, uap, retval)
   1798  1.1.1.2  fvdl 	struct proc *p;
   1799  1.1.1.2  fvdl 	struct rmdir_args *uap;
   1800  1.1.1.2  fvdl 	int *retval;
   1801  1.1.1.2  fvdl {
   1802  1.1.1.2  fvdl 	register struct vnode *vp;
   1803  1.1.1.2  fvdl 	int error;
   1804  1.1.1.2  fvdl 	struct nameidata nd;
   1805  1.1.1.2  fvdl 
   1806  1.1.1.2  fvdl 	NDINIT(&nd, DELETE, LOCKPARENT | LOCKLEAF, UIO_USERSPACE, uap->path, p);
   1807  1.1.1.2  fvdl 	if (error = namei(&nd))
   1808  1.1.1.2  fvdl 		return (error);
   1809  1.1.1.2  fvdl 	vp = nd.ni_vp;
   1810  1.1.1.2  fvdl 	if (vp->v_type != VDIR) {
   1811  1.1.1.2  fvdl 		error = ENOTDIR;
   1812  1.1.1.2  fvdl 		goto out;
   1813  1.1.1.2  fvdl 	}
   1814  1.1.1.2  fvdl 	/*
   1815  1.1.1.2  fvdl 	 * No rmdir "." please.
   1816  1.1.1.2  fvdl 	 */
   1817  1.1.1.2  fvdl 	if (nd.ni_dvp == vp) {
   1818  1.1.1.2  fvdl 		error = EINVAL;
   1819  1.1.1.2  fvdl 		goto out;
   1820  1.1.1.2  fvdl 	}
   1821  1.1.1.2  fvdl 	/*
   1822  1.1.1.2  fvdl 	 * The root of a mounted filesystem cannot be deleted.
   1823  1.1.1.2  fvdl 	 */
   1824  1.1.1.2  fvdl 	if (vp->v_flag & VROOT)
   1825  1.1.1.2  fvdl 		error = EBUSY;
   1826  1.1.1.2  fvdl out:
   1827  1.1.1.2  fvdl 	if (!error) {
   1828  1.1.1.2  fvdl 		LEASE_CHECK(nd.ni_dvp, p, p->p_ucred, LEASE_WRITE);
   1829  1.1.1.2  fvdl 		LEASE_CHECK(vp, p, p->p_ucred, LEASE_WRITE);
   1830  1.1.1.2  fvdl 		error = VOP_RMDIR(nd.ni_dvp, nd.ni_vp, &nd.ni_cnd);
   1831  1.1.1.2  fvdl 	} else {
   1832  1.1.1.2  fvdl 		VOP_ABORTOP(nd.ni_dvp, &nd.ni_cnd);
   1833  1.1.1.2  fvdl 		if (nd.ni_dvp == vp)
   1834  1.1.1.2  fvdl 			vrele(nd.ni_dvp);
   1835  1.1.1.2  fvdl 		else
   1836  1.1.1.2  fvdl 			vput(nd.ni_dvp);
   1837  1.1.1.2  fvdl 		vput(vp);
   1838  1.1.1.2  fvdl 	}
   1839  1.1.1.2  fvdl 	return (error);
   1840  1.1.1.2  fvdl }
   1841  1.1.1.2  fvdl 
   1842  1.1.1.2  fvdl #ifdef COMPAT_43
   1843  1.1.1.2  fvdl /*
   1844  1.1.1.2  fvdl  * Read a block of directory entries in a file system independent format.
   1845  1.1.1.2  fvdl  */
   1846  1.1.1.2  fvdl struct ogetdirentries_args {
   1847  1.1.1.2  fvdl 	int	fd;
   1848  1.1.1.2  fvdl 	char	*buf;
   1849  1.1.1.2  fvdl 	u_int	count;
   1850  1.1.1.2  fvdl 	long	*basep;
   1851  1.1.1.2  fvdl };
   1852  1.1.1.2  fvdl ogetdirentries(p, uap, retval)
   1853  1.1.1.2  fvdl 	struct proc *p;
   1854  1.1.1.2  fvdl 	register struct ogetdirentries_args *uap;
   1855  1.1.1.2  fvdl 	int *retval;
   1856  1.1.1.2  fvdl {
   1857  1.1.1.2  fvdl 	register struct vnode *vp;
   1858  1.1.1.2  fvdl 	struct file *fp;
   1859  1.1.1.2  fvdl 	struct uio auio, kuio;
   1860  1.1.1.2  fvdl 	struct iovec aiov, kiov;
   1861  1.1.1.2  fvdl 	struct dirent *dp, *edp;
   1862  1.1.1.2  fvdl 	caddr_t dirbuf;
   1863  1.1.1.2  fvdl 	int error, readcnt;
   1864  1.1.1.2  fvdl 	long loff;
   1865  1.1.1.2  fvdl 
   1866  1.1.1.2  fvdl 	if (error = getvnode(p->p_fd, uap->fd, &fp))
   1867  1.1.1.2  fvdl 		return (error);
   1868  1.1.1.2  fvdl 	if ((fp->f_flag & FREAD) == 0)
   1869  1.1.1.2  fvdl 		return (EBADF);
   1870  1.1.1.2  fvdl 	vp = (struct vnode *)fp->f_data;
   1871  1.1.1.2  fvdl 	if (vp->v_type != VDIR)
   1872  1.1.1.2  fvdl 		return (EINVAL);
   1873  1.1.1.2  fvdl 	aiov.iov_base = uap->buf;
   1874  1.1.1.2  fvdl 	aiov.iov_len = uap->count;
   1875  1.1.1.2  fvdl 	auio.uio_iov = &aiov;
   1876  1.1.1.2  fvdl 	auio.uio_iovcnt = 1;
   1877  1.1.1.2  fvdl 	auio.uio_rw = UIO_READ;
   1878  1.1.1.2  fvdl 	auio.uio_segflg = UIO_USERSPACE;
   1879  1.1.1.2  fvdl 	auio.uio_procp = p;
   1880  1.1.1.2  fvdl 	auio.uio_resid = uap->count;
   1881  1.1.1.2  fvdl 	VOP_LOCK(vp);
   1882  1.1.1.2  fvdl 	loff = auio.uio_offset = fp->f_offset;
   1883  1.1.1.2  fvdl #	if (BYTE_ORDER != LITTLE_ENDIAN)
   1884  1.1.1.2  fvdl 		if (vp->v_mount->mnt_maxsymlinklen <= 0) {
   1885  1.1.1.2  fvdl 			error = VOP_READDIR(vp, &auio, fp->f_cred);
   1886  1.1.1.2  fvdl 			fp->f_offset = auio.uio_offset;
   1887  1.1.1.2  fvdl 		} else
   1888  1.1.1.2  fvdl #	endif
   1889  1.1.1.2  fvdl 	{
   1890  1.1.1.2  fvdl 		kuio = auio;
   1891  1.1.1.2  fvdl 		kuio.uio_iov = &kiov;
   1892  1.1.1.2  fvdl 		kuio.uio_segflg = UIO_SYSSPACE;
   1893  1.1.1.2  fvdl 		kiov.iov_len = uap->count;
   1894  1.1.1.2  fvdl 		MALLOC(dirbuf, caddr_t, uap->count, M_TEMP, M_WAITOK);
   1895  1.1.1.2  fvdl 		kiov.iov_base = dirbuf;
   1896  1.1.1.2  fvdl 		error = VOP_READDIR(vp, &kuio, fp->f_cred);
   1897  1.1.1.2  fvdl 		fp->f_offset = kuio.uio_offset;
   1898  1.1.1.2  fvdl 		if (error == 0) {
   1899  1.1.1.2  fvdl 			readcnt = uap->count - kuio.uio_resid;
   1900  1.1.1.2  fvdl 			edp = (struct dirent *)&dirbuf[readcnt];
   1901  1.1.1.2  fvdl 			for (dp = (struct dirent *)dirbuf; dp < edp; ) {
   1902  1.1.1.2  fvdl #				if (BYTE_ORDER == LITTLE_ENDIAN)
   1903  1.1.1.2  fvdl 					/*
   1904  1.1.1.2  fvdl 					 * The expected low byte of
   1905  1.1.1.2  fvdl 					 * dp->d_namlen is our dp->d_type.
   1906  1.1.1.2  fvdl 					 * The high MBZ byte of dp->d_namlen
   1907  1.1.1.2  fvdl 					 * is our dp->d_namlen.
   1908  1.1.1.2  fvdl 					 */
   1909  1.1.1.2  fvdl 					dp->d_type = dp->d_namlen;
   1910  1.1.1.2  fvdl 					dp->d_namlen = 0;
   1911  1.1.1.2  fvdl #				else
   1912  1.1.1.2  fvdl 					/*
   1913  1.1.1.2  fvdl 					 * The dp->d_type is the high byte
   1914  1.1.1.2  fvdl 					 * of the expected dp->d_namlen,
   1915  1.1.1.2  fvdl 					 * so must be zero'ed.
   1916  1.1.1.2  fvdl 					 */
   1917  1.1.1.2  fvdl 					dp->d_type = 0;
   1918  1.1.1.2  fvdl #				endif
   1919  1.1.1.2  fvdl 				if (dp->d_reclen > 0) {
   1920  1.1.1.2  fvdl 					dp = (struct dirent *)
   1921  1.1.1.2  fvdl 					    ((char *)dp + dp->d_reclen);
   1922  1.1.1.2  fvdl 				} else {
   1923  1.1.1.2  fvdl 					error = EIO;
   1924  1.1.1.2  fvdl 					break;
   1925  1.1.1.2  fvdl 				}
   1926  1.1.1.2  fvdl 			}
   1927  1.1.1.2  fvdl 			if (dp >= edp)
   1928  1.1.1.2  fvdl 				error = uiomove(dirbuf, readcnt, &auio);
   1929  1.1.1.2  fvdl 		}
   1930  1.1.1.2  fvdl 		FREE(dirbuf, M_TEMP);
   1931  1.1.1.2  fvdl 	}
   1932  1.1.1.2  fvdl 	VOP_UNLOCK(vp);
   1933  1.1.1.2  fvdl 	if (error)
   1934  1.1.1.2  fvdl 		return (error);
   1935  1.1.1.2  fvdl 	error = copyout((caddr_t)&loff, (caddr_t)uap->basep, sizeof(long));
   1936  1.1.1.2  fvdl 	*retval = uap->count - auio.uio_resid;
   1937  1.1.1.2  fvdl 	return (error);
   1938  1.1.1.2  fvdl }
   1939  1.1.1.2  fvdl #endif
   1940  1.1.1.2  fvdl 
   1941  1.1.1.2  fvdl /*
   1942  1.1.1.2  fvdl  * Read a block of directory entries in a file system independent format.
   1943  1.1.1.2  fvdl  */
   1944  1.1.1.2  fvdl struct getdirentries_args {
   1945  1.1.1.2  fvdl 	int	fd;
   1946  1.1.1.2  fvdl 	char	*buf;
   1947  1.1.1.2  fvdl 	u_int	count;
   1948  1.1.1.2  fvdl 	long	*basep;
   1949  1.1.1.2  fvdl };
   1950  1.1.1.2  fvdl getdirentries(p, uap, retval)
   1951  1.1.1.2  fvdl 	struct proc *p;
   1952  1.1.1.2  fvdl 	register struct getdirentries_args *uap;
   1953  1.1.1.2  fvdl 	int *retval;
   1954  1.1.1.2  fvdl {
   1955  1.1.1.2  fvdl 	register struct vnode *vp;
   1956  1.1.1.2  fvdl 	struct file *fp;
   1957  1.1.1.2  fvdl 	struct uio auio;
   1958  1.1.1.2  fvdl 	struct iovec aiov;
   1959  1.1.1.2  fvdl 	long loff;
   1960  1.1.1.2  fvdl 	int error;
   1961  1.1.1.2  fvdl 
   1962  1.1.1.2  fvdl 	if (error = getvnode(p->p_fd, uap->fd, &fp))
   1963  1.1.1.2  fvdl 		return (error);
   1964  1.1.1.2  fvdl 	if ((fp->f_flag & FREAD) == 0)
   1965  1.1.1.2  fvdl 		return (EBADF);
   1966  1.1.1.2  fvdl 	vp = (struct vnode *)fp->f_data;
   1967  1.1.1.2  fvdl unionread:
   1968  1.1.1.2  fvdl 	if (vp->v_type != VDIR)
   1969  1.1.1.2  fvdl 		return (EINVAL);
   1970  1.1.1.2  fvdl 	aiov.iov_base = uap->buf;
   1971  1.1.1.2  fvdl 	aiov.iov_len = uap->count;
   1972  1.1.1.2  fvdl 	auio.uio_iov = &aiov;
   1973  1.1.1.2  fvdl 	auio.uio_iovcnt = 1;
   1974  1.1.1.2  fvdl 	auio.uio_rw = UIO_READ;
   1975  1.1.1.2  fvdl 	auio.uio_segflg = UIO_USERSPACE;
   1976  1.1.1.2  fvdl 	auio.uio_procp = p;
   1977  1.1.1.2  fvdl 	auio.uio_resid = uap->count;
   1978  1.1.1.2  fvdl 	VOP_LOCK(vp);
   1979  1.1.1.2  fvdl 	loff = auio.uio_offset = fp->f_offset;
   1980  1.1.1.2  fvdl 	error = VOP_READDIR(vp, &auio, fp->f_cred);
   1981  1.1.1.2  fvdl 	fp->f_offset = auio.uio_offset;
   1982  1.1.1.2  fvdl 	VOP_UNLOCK(vp);
   1983  1.1.1.2  fvdl 	if (error)
   1984  1.1.1.2  fvdl 		return (error);
   1985  1.1.1.2  fvdl 
   1986  1.1.1.2  fvdl #ifdef UNION
   1987  1.1.1.2  fvdl {
   1988  1.1.1.2  fvdl 	extern int (**union_vnodeop_p)();
   1989  1.1.1.2  fvdl 	extern struct vnode *union_lowervp __P((struct vnode *));
   1990  1.1.1.2  fvdl 
   1991  1.1.1.2  fvdl 	if ((uap->count == auio.uio_resid) &&
   1992  1.1.1.2  fvdl 	    (vp->v_op == union_vnodeop_p)) {
   1993  1.1.1.2  fvdl 		struct vnode *tvp = vp;
   1994  1.1.1.2  fvdl 
   1995  1.1.1.2  fvdl 		vp = union_lowervp(vp);
   1996  1.1.1.2  fvdl 		if (vp != NULLVP) {
   1997  1.1.1.2  fvdl 			VOP_LOCK(vp);
   1998  1.1.1.2  fvdl 			error = VOP_OPEN(vp, FREAD);
   1999  1.1.1.2  fvdl 			VOP_UNLOCK(vp);
   2000  1.1.1.2  fvdl 
   2001  1.1.1.2  fvdl 			if (error) {
   2002  1.1.1.2  fvdl 				vrele(vp);
   2003  1.1.1.2  fvdl 				return (error);
   2004  1.1.1.2  fvdl 			}
   2005  1.1.1.2  fvdl 			fp->f_data = (caddr_t) vp;
   2006  1.1.1.2  fvdl 			fp->f_offset = 0;
   2007  1.1.1.2  fvdl 			error = vn_close(tvp, FREAD, fp->f_cred, p);
   2008  1.1.1.2  fvdl 			if (error)
   2009  1.1.1.2  fvdl 				return (error);
   2010  1.1.1.2  fvdl 			goto unionread;
   2011  1.1.1.2  fvdl 		}
   2012  1.1.1.2  fvdl 	}
   2013  1.1.1.2  fvdl }
   2014  1.1.1.2  fvdl #endif
   2015  1.1.1.2  fvdl 
   2016  1.1.1.2  fvdl 	if ((uap->count == auio.uio_resid) &&
   2017  1.1.1.2  fvdl 	    (vp->v_flag & VROOT) &&
   2018  1.1.1.2  fvdl 	    (vp->v_mount->mnt_flag & MNT_UNION)) {
   2019  1.1.1.2  fvdl 		struct vnode *tvp = vp;
   2020  1.1.1.2  fvdl 		vp = vp->v_mount->mnt_vnodecovered;
   2021  1.1.1.2  fvdl 		VREF(vp);
   2022  1.1.1.2  fvdl 		fp->f_data = (caddr_t) vp;
   2023  1.1.1.2  fvdl 		fp->f_offset = 0;
   2024  1.1.1.2  fvdl 		vrele(tvp);
   2025  1.1.1.2  fvdl 		goto unionread;
   2026  1.1.1.2  fvdl 	}
   2027  1.1.1.2  fvdl 	error = copyout((caddr_t)&loff, (caddr_t)uap->basep, sizeof(long));
   2028  1.1.1.2  fvdl 	*retval = uap->count - auio.uio_resid;
   2029  1.1.1.2  fvdl 	return (error);
   2030  1.1.1.2  fvdl }
   2031  1.1.1.2  fvdl 
   2032  1.1.1.2  fvdl /*
   2033  1.1.1.2  fvdl  * Set the mode mask for creation of filesystem nodes.
   2034  1.1.1.2  fvdl  */
   2035  1.1.1.2  fvdl struct umask_args {
   2036  1.1.1.2  fvdl 	int	newmask;
   2037  1.1.1.2  fvdl };
   2038  1.1.1.2  fvdl mode_t				/* XXX */
   2039  1.1.1.2  fvdl umask(p, uap, retval)
   2040  1.1.1.2  fvdl 	struct proc *p;
   2041  1.1.1.2  fvdl 	struct umask_args *uap;
   2042  1.1.1.2  fvdl 	int *retval;
   2043  1.1.1.2  fvdl {
   2044  1.1.1.2  fvdl 	register struct filedesc *fdp;
   2045  1.1.1.2  fvdl 
   2046  1.1.1.2  fvdl 	fdp = p->p_fd;
   2047  1.1.1.2  fvdl 	*retval = fdp->fd_cmask;
   2048  1.1.1.2  fvdl 	fdp->fd_cmask = uap->newmask & ALLPERMS;
   2049  1.1.1.2  fvdl 	return (0);
   2050  1.1.1.2  fvdl }
   2051  1.1.1.2  fvdl 
   2052  1.1.1.2  fvdl /*
   2053  1.1.1.2  fvdl  * Void all references to file by ripping underlying filesystem
   2054  1.1.1.2  fvdl  * away from vnode.
   2055  1.1.1.2  fvdl  */
   2056  1.1.1.2  fvdl struct revoke_args {
   2057  1.1.1.2  fvdl 	char	*path;
   2058  1.1.1.2  fvdl };
   2059  1.1.1.2  fvdl /* ARGSUSED */
   2060  1.1.1.2  fvdl revoke(p, uap, retval)
   2061  1.1.1.2  fvdl 	struct proc *p;
   2062  1.1.1.2  fvdl 	register struct revoke_args *uap;
   2063  1.1.1.2  fvdl 	int *retval;
   2064  1.1.1.2  fvdl {
   2065  1.1.1.2  fvdl 	register struct vnode *vp;
   2066  1.1.1.2  fvdl 	struct vattr vattr;
   2067  1.1.1.2  fvdl 	int error;
   2068  1.1.1.2  fvdl 	struct nameidata nd;
   2069  1.1.1.2  fvdl 
   2070  1.1.1.2  fvdl 	NDINIT(&nd, LOOKUP, FOLLOW, UIO_USERSPACE, uap->path, p);
   2071  1.1.1.2  fvdl 	if (error = namei(&nd))
   2072  1.1.1.2  fvdl 		return (error);
   2073  1.1.1.2  fvdl 	vp = nd.ni_vp;
   2074  1.1.1.2  fvdl 	if (vp->v_type != VCHR && vp->v_type != VBLK) {
   2075  1.1.1.2  fvdl 		error = EINVAL;
   2076  1.1.1.2  fvdl 		goto out;
   2077  1.1.1.2  fvdl 	}
   2078  1.1.1.2  fvdl 	if (error = VOP_GETATTR(vp, &vattr, p->p_ucred, p))
   2079  1.1.1.2  fvdl 		goto out;
   2080  1.1.1.2  fvdl 	if (p->p_ucred->cr_uid != vattr.va_uid &&
   2081  1.1.1.2  fvdl 	    (error = suser(p->p_ucred, &p->p_acflag)))
   2082  1.1.1.2  fvdl 		goto out;
   2083  1.1.1.2  fvdl 	if (vp->v_usecount > 1 || (vp->v_flag & VALIASED))
   2084  1.1.1.2  fvdl 		vgoneall(vp);
   2085  1.1.1.2  fvdl out:
   2086  1.1.1.2  fvdl 	vrele(vp);
   2087  1.1.1.2  fvdl 	return (error);
   2088  1.1.1.2  fvdl }
   2089  1.1.1.2  fvdl 
   2090  1.1.1.2  fvdl /*
   2091  1.1.1.2  fvdl  * Convert a user file descriptor to a kernel file entry.
   2092  1.1.1.2  fvdl  */
   2093  1.1.1.2  fvdl getvnode(fdp, fd, fpp)
   2094  1.1.1.2  fvdl 	struct filedesc *fdp;
   2095  1.1.1.2  fvdl 	struct file **fpp;
   2096  1.1.1.2  fvdl 	int fd;
   2097  1.1.1.2  fvdl {
   2098  1.1.1.2  fvdl 	struct file *fp;
   2099  1.1.1.2  fvdl 
   2100  1.1.1.2  fvdl 	if ((u_int)fd >= fdp->fd_nfiles ||
   2101  1.1.1.2  fvdl 	    (fp = fdp->fd_ofiles[fd]) == NULL)
   2102  1.1.1.2  fvdl 		return (EBADF);
   2103  1.1.1.2  fvdl 	if (fp->f_type != DTYPE_VNODE)
   2104  1.1.1.2  fvdl 		return (EINVAL);
   2105  1.1.1.2  fvdl 	*fpp = fp;
   2106  1.1.1.2  fvdl 	return (0);
   2107  1.1.1.2  fvdl }
   2108