Home | History | Annotate | Line # | Download | only in kern
vfs_syscalls.c revision 1.147.4.5
      1 /*	$NetBSD: vfs_syscalls.c,v 1.147.4.5 1999/10/26 19:15:17 fvdl Exp $	*/
      2 
      3 /*
      4  * Copyright (c) 1989, 1993
      5  *	The Regents of the University of California.  All rights reserved.
      6  * (c) UNIX System Laboratories, Inc.
      7  * All or some portions of this file are derived from material licensed
      8  * to the University of California by American Telephone and Telegraph
      9  * Co. or Unix System Laboratories, Inc. and are reproduced herein with
     10  * the permission of UNIX System Laboratories, Inc.
     11  *
     12  * Redistribution and use in source and binary forms, with or without
     13  * modification, are permitted provided that the following conditions
     14  * are met:
     15  * 1. Redistributions of source code must retain the above copyright
     16  *    notice, this list of conditions and the following disclaimer.
     17  * 2. Redistributions in binary form must reproduce the above copyright
     18  *    notice, this list of conditions and the following disclaimer in the
     19  *    documentation and/or other materials provided with the distribution.
     20  * 3. All advertising materials mentioning features or use of this software
     21  *    must display the following acknowledgement:
     22  *	This product includes software developed by the University of
     23  *	California, Berkeley and its contributors.
     24  * 4. Neither the name of the University nor the names of its contributors
     25  *    may be used to endorse or promote products derived from this software
     26  *    without specific prior written permission.
     27  *
     28  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     29  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     30  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     31  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     32  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     33  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     34  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     35  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     36  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     37  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     38  * SUCH DAMAGE.
     39  *
     40  *	@(#)vfs_syscalls.c	8.42 (Berkeley) 7/31/95
     41  */
     42 
     43 #include "opt_compat_netbsd.h"
     44 #include "opt_compat_43.h"
     45 
     46 #include <sys/param.h>
     47 #include <sys/systm.h>
     48 #include <sys/namei.h>
     49 #include <sys/filedesc.h>
     50 #include <sys/kernel.h>
     51 #include <sys/file.h>
     52 #include <sys/stat.h>
     53 #include <sys/vnode.h>
     54 #include <sys/mount.h>
     55 #include <sys/proc.h>
     56 #include <sys/uio.h>
     57 #include <sys/malloc.h>
     58 #include <sys/dirent.h>
     59 
     60 #include <sys/syscallargs.h>
     61 
     62 #include <vm/vm.h>
     63 #include <sys/sysctl.h>
     64 
     65 #include <miscfs/genfs/genfs.h>
     66 #include <miscfs/syncfs/syncfs.h>
     67 
     68 #include <uvm/uvm_extern.h>
     69 
     70 static int change_dir __P((struct nameidata *, struct proc *));
     71 static int change_mode __P((struct vnode *, int, struct proc *p));
     72 static int change_owner __P((struct vnode *, uid_t, gid_t, struct proc *,
     73     int));
     74 static int change_utimes __P((struct vnode *vp, const struct timeval *,
     75 	       struct proc *p));
     76 static int rename_files __P((const char *, const char *, struct proc *, int));
     77 
     78 void checkdirs __P((struct vnode *));
     79 int dounmount __P((struct mount *, int, struct proc *));
     80 
     81 /*
     82  * Virtual File System System Calls
     83  */
     84 
     85 /*
     86  * Mount a file system.
     87  */
     88 
     89 /*
     90  * This table is used to maintain compatibility with 4.3BSD
     91  * and NetBSD 0.9 mount syscalls.  Note, the order is important!
     92  *
     93  * Also note that not all of these had actual numbers in 4.3BSD
     94  * or NetBSD 0.9!
     95  */
     96 const char *mountcompatnames[] = {
     97 	NULL,		/* 0 = MOUNT_NONE */
     98 	MOUNT_FFS,	/* 1 */
     99 	MOUNT_NFS,	/* 2 */
    100 	MOUNT_MFS,	/* 3 */
    101 	MOUNT_MSDOS,	/* 4 */
    102 	MOUNT_LFS,	/* 5 */
    103 	NULL,		/* 6 = MOUNT_LOFS */
    104 	MOUNT_FDESC,	/* 7 */
    105 	MOUNT_PORTAL,	/* 8 */
    106 	MOUNT_NULL,	/* 9 */
    107 	MOUNT_UMAP,	/* 10 */
    108 	MOUNT_KERNFS,	/* 11 */
    109 	MOUNT_PROCFS,	/* 12 */
    110 	MOUNT_AFS,	/* 13 */
    111 	MOUNT_CD9660,	/* 14 = MOUNT_ISOFS */
    112 	MOUNT_UNION,	/* 15 */
    113 	MOUNT_ADOSFS,	/* 16 */
    114 	MOUNT_EXT2FS,	/* 17 */
    115 	MOUNT_CODA,	/* 18 */
    116 	MOUNT_FILECORE,	/* 19 */
    117 	MOUNT_NTFS,	/* 20 */
    118 };
    119 const int nmountcompatnames = sizeof(mountcompatnames) /
    120     sizeof(mountcompatnames[0]);
    121 
    122 /* ARGSUSED */
    123 int
    124 sys_mount(p, v, retval)
    125 	struct proc *p;
    126 	void *v;
    127 	register_t *retval;
    128 {
    129 	register struct sys_mount_args /* {
    130 		syscallarg(const char *) type;
    131 		syscallarg(const char *) path;
    132 		syscallarg(int) flags;
    133 		syscallarg(void *) data;
    134 	} */ *uap = v;
    135 	struct vnode *vp;
    136 	struct mount *mp;
    137 	int error, flag = 0;
    138 	char fstypename[MFSNAMELEN];
    139 	struct vattr va;
    140 	struct nameidata nd;
    141 	struct vfsops *vfs;
    142 
    143 	/*
    144 	 * Get vnode to be covered
    145 	 */
    146 	NDINIT(&nd, LOOKUP, FOLLOW , UIO_USERSPACE,
    147 	    SCARG(uap, path), p);
    148 	if ((error = namei(&nd)) != 0)
    149 		return (error);
    150 	vp = nd.ni_vp;
    151 	/*
    152 	 * A lookup in VFS_MOUNT might result in an attempt to
    153 	 * lock this vnode again, so make the lock resursive.
    154 	 */
    155 	vn_lock(vp, LK_EXCLUSIVE | LK_RETRY | LK_SETRECURSE);
    156 	if (SCARG(uap, flags) & MNT_UPDATE) {
    157 		if ((vp->v_flag & VROOT) == 0) {
    158 			vput(vp);
    159 			return (EINVAL);
    160 		}
    161 		mp = vp->v_mount;
    162 		flag = mp->mnt_flag;
    163 		vfs = mp->mnt_op;
    164 		/*
    165 		 * We only allow the filesystem to be reloaded if it
    166 		 * is currently mounted read-only.
    167 		 */
    168 		if ((SCARG(uap, flags) & MNT_RELOAD) &&
    169 		    ((mp->mnt_flag & MNT_RDONLY) == 0)) {
    170 			vput(vp);
    171 			return (EOPNOTSUPP);	/* Needs translation */
    172 		}
    173 		/*
    174 		 * In "highly secure" mode, don't let the caller do anything
    175 		 * but downgrade a filesystem from read-write to read-only.
    176 		 * (see also below; MNT_UPDATE is required.)
    177 		 */
    178 		if (securelevel >= 2 &&
    179 		    (SCARG(uap, flags) !=
    180 		    (mp->mnt_flag | MNT_RDONLY |
    181 		    MNT_RELOAD | MNT_FORCE | MNT_UPDATE))) {
    182 			vput(vp);
    183 			return (EPERM);
    184 		}
    185 		mp->mnt_flag |=
    186 		    SCARG(uap, flags) & (MNT_RELOAD | MNT_FORCE | MNT_UPDATE);
    187 		/*
    188 		 * Only root, or the user that did the original mount is
    189 		 * permitted to update it.
    190 		 */
    191 		if (mp->mnt_stat.f_owner != p->p_ucred->cr_uid &&
    192 		    (error = suser(p->p_ucred, &p->p_acflag)) != 0) {
    193 			vput(vp);
    194 			return (error);
    195 		}
    196 		/*
    197 		 * Do not allow NFS export by non-root users. For non-root
    198 		 * users, silently enforce MNT_NOSUID and MNT_NODEV, and
    199 		 * MNT_NOEXEC if mount point is already MNT_NOEXEC.
    200 		 */
    201 		if (p->p_ucred->cr_uid != 0) {
    202 			if (SCARG(uap, flags) & MNT_EXPORTED) {
    203 				vput(vp);
    204 				return (EPERM);
    205 			}
    206 			SCARG(uap, flags) |= MNT_NOSUID | MNT_NODEV;
    207 			if (flag & MNT_NOEXEC)
    208 				SCARG(uap, flags) |= MNT_NOEXEC;
    209 		}
    210 		if (vfs_busy(mp, LK_NOWAIT, 0)) {
    211 			vput(vp);
    212 			return (EPERM);
    213 		}
    214 		VOP_UNLOCK(vp, 0);
    215 		goto update;
    216 	} else {
    217 		if (securelevel >= 2)
    218 			return (EPERM);
    219 	}
    220 	/*
    221 	 * If the user is not root, ensure that they own the directory
    222 	 * onto which we are attempting to mount.
    223 	 */
    224 	if ((error = VOP_GETATTR(vp, &va, p->p_ucred, p)) != 0 ||
    225 	    (va.va_uid != p->p_ucred->cr_uid &&
    226 		(error = suser(p->p_ucred, &p->p_acflag)) != 0)) {
    227 		vput(vp);
    228 		return (error);
    229 	}
    230 	/*
    231 	 * Do not allow NFS export by non-root users. For non-root users,
    232 	 * silently enforce MNT_NOSUID and MNT_NODEV, and MNT_NOEXEC if the
    233 	 * mount point is already MNT_NOEXEC.
    234 	 */
    235 	if (p->p_ucred->cr_uid != 0) {
    236 		if (SCARG(uap, flags) & MNT_EXPORTED) {
    237 			vput(vp);
    238 			return (EPERM);
    239 		}
    240 		SCARG(uap, flags) |= MNT_NOSUID | MNT_NODEV;
    241 		if (vp->v_mount->mnt_flag & MNT_NOEXEC)
    242 			SCARG(uap, flags) |= MNT_NOEXEC;
    243 	}
    244 	if ((error = vinvalbuf(vp, V_SAVE, p->p_ucred, p, 0, 0)) != 0)
    245 		return (error);
    246 	if (vp->v_type != VDIR) {
    247 		vput(vp);
    248 		return (ENOTDIR);
    249 	}
    250 	error = copyinstr(SCARG(uap, type), fstypename, MFSNAMELEN, NULL);
    251 	if (error) {
    252 #if defined(COMPAT_09) || defined(COMPAT_43)
    253 		/*
    254 		 * Historically filesystem types were identified by number.
    255 		 * If we get an integer for the filesystem type instead of a
    256 		 * string, we check to see if it matches one of the historic
    257 		 * filesystem types.
    258 		 */
    259 		u_long fsindex = (u_long)SCARG(uap, type);
    260 		if (fsindex >= nmountcompatnames ||
    261 		    mountcompatnames[fsindex] == NULL) {
    262 			vput(vp);
    263 			return (ENODEV);
    264 		}
    265 		strncpy(fstypename, mountcompatnames[fsindex], MFSNAMELEN);
    266 #else
    267 		vput(vp);
    268 		return (error);
    269 #endif
    270 	}
    271 #ifdef	COMPAT_10
    272 	/* Accept `ufs' as an alias for `ffs'. */
    273 	if (!strncmp(fstypename, "ufs", MFSNAMELEN))
    274 		strncpy(fstypename, "ffs", MFSNAMELEN);
    275 #endif
    276 	if ((vfs = vfs_getopsbyname(fstypename)) == NULL) {
    277 		vput(vp);
    278 		return (ENODEV);
    279 	}
    280 	if (vp->v_mountedhere != NULL) {
    281 		vput(vp);
    282 		return (EBUSY);
    283 	}
    284 
    285 	/*
    286 	 * Allocate and initialize the file system.
    287 	 */
    288 	mp = (struct mount *)malloc((u_long)sizeof(struct mount),
    289 		M_MOUNT, M_WAITOK);
    290 	memset((char *)mp, 0, (u_long)sizeof(struct mount));
    291 	lockinit(&mp->mnt_lock, PVFS, "vfslock", 0, 0);
    292 	(void)vfs_busy(mp, LK_NOWAIT, 0);
    293 	mp->mnt_op = vfs;
    294 	vfs->vfs_refcount++;
    295 	mp->mnt_vnodecovered = vp;
    296 	mp->mnt_stat.f_owner = p->p_ucred->cr_uid;
    297 	mp->mnt_unmounter = NULL;
    298 update:
    299 	/*
    300 	 * Set the mount level flags.
    301 	 */
    302 	if (SCARG(uap, flags) & MNT_RDONLY)
    303 		mp->mnt_flag |= MNT_RDONLY;
    304 	else if (mp->mnt_flag & MNT_RDONLY)
    305 		mp->mnt_flag |= MNT_WANTRDWR;
    306 	mp->mnt_flag &=~ (MNT_NOSUID | MNT_NOEXEC | MNT_NODEV |
    307 	    MNT_SYNCHRONOUS | MNT_UNION | MNT_ASYNC | MNT_NOCOREDUMP |
    308 	    MNT_NOATIME | MNT_NODEVMTIME | MNT_SYMPERM);
    309 	mp->mnt_flag |= SCARG(uap, flags) & (MNT_NOSUID | MNT_NOEXEC |
    310 	    MNT_NODEV | MNT_SYNCHRONOUS | MNT_UNION | MNT_ASYNC |
    311 	    MNT_NOCOREDUMP | MNT_NOATIME | MNT_NODEVMTIME | MNT_SYMPERM);
    312 	/*
    313 	 * Mount the filesystem.
    314 	 */
    315 	error = VFS_MOUNT(mp, SCARG(uap, path), SCARG(uap, data), &nd, p);
    316 	if (mp->mnt_flag & MNT_UPDATE) {
    317 		vrele(vp);
    318 		if (mp->mnt_flag & MNT_WANTRDWR)
    319 			mp->mnt_flag &= ~MNT_RDONLY;
    320 		mp->mnt_flag &=~
    321 		    (MNT_UPDATE | MNT_RELOAD | MNT_FORCE | MNT_WANTRDWR);
    322 		if (error)
    323 			mp->mnt_flag = flag;
    324 		if ((mp->mnt_flag & MNT_RDONLY) == 0) {
    325 			if (mp->mnt_syncer == NULL)
    326 				error = vfs_allocate_syncvnode(mp);
    327 		} else {
    328 			if (mp->mnt_syncer != NULL)
    329 				vgone(mp->mnt_syncer);
    330 			mp->mnt_syncer = NULL;
    331 		}
    332 		vfs_unbusy(mp);
    333 		return (error);
    334 	}
    335 	/*
    336 	 * Put the new filesystem on the mount list after root.
    337 	 */
    338 	cache_purge(vp);
    339 	if (!error) {
    340 		vp->v_mountedhere = mp;
    341 		simple_lock(&mountlist_slock);
    342 		CIRCLEQ_INSERT_TAIL(&mountlist, mp, mnt_list);
    343 		simple_unlock(&mountlist_slock);
    344 		checkdirs(vp);
    345 		VOP_UNLOCK(vp, 0);
    346 		if ((mp->mnt_flag & MNT_RDONLY) == 0)
    347 			error = vfs_allocate_syncvnode(mp);
    348 		vfs_unbusy(mp);
    349 		(void) VFS_STATFS(mp, &mp->mnt_stat, p);
    350 		if ((error = VFS_START(mp, 0, p)))
    351 			vrele(vp);
    352 	} else {
    353 		vp->v_mountedhere = (struct mount *)0;
    354 		vfs->vfs_refcount--;
    355 		vfs_unbusy(mp);
    356 		free((caddr_t)mp, M_MOUNT);
    357 		vput(vp);
    358 	}
    359 	return (error);
    360 }
    361 
    362 /*
    363  * Scan all active processes to see if any of them have a current
    364  * or root directory onto which the new filesystem has just been
    365  * mounted. If so, replace them with the new mount point.
    366  */
    367 void
    368 checkdirs(olddp)
    369 	struct vnode *olddp;
    370 {
    371 	struct cwdinfo *cwdi;
    372 	struct vnode *newdp;
    373 	struct proc *p;
    374 
    375 	if (olddp->v_usecount == 1)
    376 		return;
    377 	if (VFS_ROOT(olddp->v_mountedhere, &newdp))
    378 		panic("mount: lost mount");
    379 	proclist_lock_read();
    380 	for (p = allproc.lh_first; p != 0; p = p->p_list.le_next) {
    381 		cwdi = p->p_cwdi;
    382 		if (cwdi->cwdi_cdir == olddp) {
    383 			vrele(cwdi->cwdi_cdir);
    384 			VREF(newdp);
    385 			cwdi->cwdi_cdir = newdp;
    386 		}
    387 		if (cwdi->cwdi_rdir == olddp) {
    388 			vrele(cwdi->cwdi_rdir);
    389 			VREF(newdp);
    390 			cwdi->cwdi_rdir = newdp;
    391 		}
    392 	}
    393 	proclist_unlock_read();
    394 	if (rootvnode == olddp) {
    395 		vrele(rootvnode);
    396 		VREF(newdp);
    397 		rootvnode = newdp;
    398 	}
    399 	vput(newdp);
    400 }
    401 
    402 /*
    403  * Unmount a file system.
    404  *
    405  * Note: unmount takes a path to the vnode mounted on as argument,
    406  * not special file (as before).
    407  */
    408 /* ARGSUSED */
    409 int
    410 sys_unmount(p, v, retval)
    411 	struct proc *p;
    412 	void *v;
    413 	register_t *retval;
    414 {
    415 	register struct sys_unmount_args /* {
    416 		syscallarg(const char *) path;
    417 		syscallarg(int) flags;
    418 	} */ *uap = v;
    419 	register struct vnode *vp;
    420 	struct mount *mp;
    421 	int error;
    422 	struct nameidata nd;
    423 
    424 	NDINIT(&nd, LOOKUP, FOLLOW | LOCKLEAF, UIO_USERSPACE,
    425 	    SCARG(uap, path), p);
    426 	if ((error = namei(&nd)) != 0)
    427 		return (error);
    428 	vp = nd.ni_vp;
    429 	mp = vp->v_mount;
    430 
    431 	/*
    432 	 * Only root, or the user that did the original mount is
    433 	 * permitted to unmount this filesystem.
    434 	 */
    435 	if ((mp->mnt_stat.f_owner != p->p_ucred->cr_uid) &&
    436 	    (error = suser(p->p_ucred, &p->p_acflag)) != 0) {
    437 		vput(vp);
    438 		return (error);
    439 	}
    440 
    441 	/*
    442 	 * Don't allow unmounting the root file system.
    443 	 */
    444 	if (mp->mnt_flag & MNT_ROOTFS) {
    445 		vput(vp);
    446 		return (EINVAL);
    447 	}
    448 
    449 	/*
    450 	 * Must be the root of the filesystem
    451 	 */
    452 	if ((vp->v_flag & VROOT) == 0) {
    453 		vput(vp);
    454 		return (EINVAL);
    455 	}
    456 	vput(vp);
    457 
    458 	if (vfs_busy(mp, 0, 0))
    459 		return (EBUSY);
    460 
    461 	return (dounmount(mp, SCARG(uap, flags), p));
    462 }
    463 
    464 /*
    465  * Do the actual file system unmount. File system is assumed to have been
    466  * marked busy by the caller.
    467  */
    468 int
    469 dounmount(mp, flags, p)
    470 	register struct mount *mp;
    471 	int flags;
    472 	struct proc *p;
    473 {
    474 	struct vnode *coveredvp;
    475 	int error;
    476 	int async;
    477 
    478 	simple_lock(&mountlist_slock);
    479 	vfs_unbusy(mp);
    480 	/*
    481 	 * XXX Freeze syncer. This should really be done on a mountpoint
    482 	 * basis, but especially the softdep code possibly called from
    483 	 * the syncer doesn't exactly work on a per-mountpoint basis,
    484 	 * so the softdep code would become a maze of vfs_busy calls.
    485 	 */
    486 	lockmgr(&syncer_lock, LK_EXCLUSIVE, NULL);
    487 
    488 	mp->mnt_flag |= MNT_UNMOUNT;
    489 	mp->mnt_unmounter = p;
    490 	lockmgr(&mp->mnt_lock, LK_DRAIN | LK_INTERLOCK, &mountlist_slock);
    491 	if (mp->mnt_flag & MNT_EXPUBLIC)
    492 		vfs_setpublicfs(NULL, NULL, NULL);
    493 	async = mp->mnt_flag & MNT_ASYNC;
    494 	mp->mnt_flag &=~ MNT_ASYNC;
    495 	cache_purgevfs(mp);	/* remove cache entries for this file sys */
    496 	if (mp->mnt_syncer != NULL)
    497 		vgone(mp->mnt_syncer);
    498 	if (((mp->mnt_flag & MNT_RDONLY) ||
    499 	    (error = VFS_SYNC(mp, MNT_WAIT, p->p_ucred, p)) == 0) ||
    500 	    (flags & MNT_FORCE))
    501 		error = VFS_UNMOUNT(mp, flags, p);
    502 	simple_lock(&mountlist_slock);
    503 	if (error) {
    504 		if ((mp->mnt_flag & MNT_RDONLY) == 0 && mp->mnt_syncer == NULL)
    505 			(void) vfs_allocate_syncvnode(mp);
    506 		mp->mnt_flag &= ~MNT_UNMOUNT;
    507 		mp->mnt_unmounter = NULL;
    508 		mp->mnt_flag |= async;
    509 		lockmgr(&mp->mnt_lock, LK_RELEASE | LK_INTERLOCK | LK_REENABLE,
    510 		    &mountlist_slock);
    511 		lockmgr(&syncer_lock, LK_RELEASE, NULL);
    512 		while(mp->mnt_wcnt > 0) {
    513 			wakeup((caddr_t)mp);
    514 			tsleep(&mp->mnt_wcnt, PVFS, "mntwcnt1", 0);
    515 		}
    516 		return (error);
    517 	}
    518 	CIRCLEQ_REMOVE(&mountlist, mp, mnt_list);
    519 	if ((coveredvp = mp->mnt_vnodecovered) != NULLVP) {
    520 		coveredvp->v_mountedhere = NULL;
    521 		vrele(coveredvp);
    522 	}
    523 	mp->mnt_op->vfs_refcount--;
    524 	if (mp->mnt_vnodelist.lh_first != NULL)
    525 		panic("unmount: dangling vnode");
    526 	mp->mnt_flag |= MNT_GONE;
    527 	lockmgr(&mp->mnt_lock, LK_RELEASE | LK_INTERLOCK, &mountlist_slock);
    528 	lockmgr(&syncer_lock, LK_RELEASE, NULL);
    529 	while(mp->mnt_wcnt > 0) {
    530 		wakeup((caddr_t)mp);
    531 		tsleep(&mp->mnt_wcnt, PVFS, "mntwcnt2", 0);
    532 	}
    533 	free((caddr_t)mp, M_MOUNT);
    534 	return (0);
    535 }
    536 
    537 /*
    538  * Sync each mounted filesystem.
    539  */
    540 #ifdef DEBUG
    541 int syncprt = 0;
    542 struct ctldebug debug0 = { "syncprt", &syncprt };
    543 #endif
    544 
    545 /* ARGSUSED */
    546 int
    547 sys_sync(p, v, retval)
    548 	struct proc *p;
    549 	void *v;
    550 	register_t *retval;
    551 {
    552 	register struct mount *mp, *nmp;
    553 	int asyncflag;
    554 
    555 	simple_lock(&mountlist_slock);
    556 	for (mp = mountlist.cqh_last; mp != (void *)&mountlist; mp = nmp) {
    557 		if (vfs_busy(mp, LK_NOWAIT, &mountlist_slock)) {
    558 			nmp = mp->mnt_list.cqe_prev;
    559 			continue;
    560 		}
    561 		if ((mp->mnt_flag & MNT_RDONLY) == 0) {
    562 			asyncflag = mp->mnt_flag & MNT_ASYNC;
    563 			mp->mnt_flag &= ~MNT_ASYNC;
    564 			uvm_vnp_sync(mp);
    565 			VFS_SYNC(mp, MNT_NOWAIT, p->p_ucred, p);
    566 			if (asyncflag)
    567 				 mp->mnt_flag |= MNT_ASYNC;
    568 		}
    569 		simple_lock(&mountlist_slock);
    570 		nmp = mp->mnt_list.cqe_prev;
    571 		vfs_unbusy(mp);
    572 
    573 	}
    574 	simple_unlock(&mountlist_slock);
    575 #ifdef DEBUG
    576 	if (syncprt)
    577 		vfs_bufstats();
    578 #endif /* DEBUG */
    579 	return (0);
    580 }
    581 
    582 /*
    583  * Change filesystem quotas.
    584  */
    585 /* ARGSUSED */
    586 int
    587 sys_quotactl(p, v, retval)
    588 	struct proc *p;
    589 	void *v;
    590 	register_t *retval;
    591 {
    592 	register struct sys_quotactl_args /* {
    593 		syscallarg(const char *) path;
    594 		syscallarg(int) cmd;
    595 		syscallarg(int) uid;
    596 		syscallarg(caddr_t) arg;
    597 	} */ *uap = v;
    598 	register struct mount *mp;
    599 	int error;
    600 	struct nameidata nd;
    601 
    602 	NDINIT(&nd, LOOKUP, FOLLOW, UIO_USERSPACE, SCARG(uap, path), p);
    603 	if ((error = namei(&nd)) != 0)
    604 		return (error);
    605 	mp = nd.ni_vp->v_mount;
    606 	vrele(nd.ni_vp);
    607 	return (VFS_QUOTACTL(mp, SCARG(uap, cmd), SCARG(uap, uid),
    608 	    SCARG(uap, arg), p));
    609 }
    610 
    611 /*
    612  * Get filesystem statistics.
    613  */
    614 /* ARGSUSED */
    615 int
    616 sys_statfs(p, v, retval)
    617 	struct proc *p;
    618 	void *v;
    619 	register_t *retval;
    620 {
    621 	register struct sys_statfs_args /* {
    622 		syscallarg(const char *) path;
    623 		syscallarg(struct statfs *) buf;
    624 	} */ *uap = v;
    625 	register struct mount *mp;
    626 	register struct statfs *sp;
    627 	int error;
    628 	struct nameidata nd;
    629 
    630 	NDINIT(&nd, LOOKUP, FOLLOW, UIO_USERSPACE, SCARG(uap, path), p);
    631 	if ((error = namei(&nd)) != 0)
    632 		return (error);
    633 	mp = nd.ni_vp->v_mount;
    634 	sp = &mp->mnt_stat;
    635 	vrele(nd.ni_vp);
    636 	if ((error = VFS_STATFS(mp, sp, p)) != 0)
    637 		return (error);
    638 	sp->f_flags = mp->mnt_flag & MNT_VISFLAGMASK;
    639 	return (copyout(sp, SCARG(uap, buf), sizeof(*sp)));
    640 }
    641 
    642 /*
    643  * Get filesystem statistics.
    644  */
    645 /* ARGSUSED */
    646 int
    647 sys_fstatfs(p, v, retval)
    648 	struct proc *p;
    649 	void *v;
    650 	register_t *retval;
    651 {
    652 	register struct sys_fstatfs_args /* {
    653 		syscallarg(int) fd;
    654 		syscallarg(struct statfs *) buf;
    655 	} */ *uap = v;
    656 	struct file *fp;
    657 	struct mount *mp;
    658 	register struct statfs *sp;
    659 	int error;
    660 
    661 	/* getvnode() will use the descriptor for us */
    662 	if ((error = getvnode(p->p_fd, SCARG(uap, fd), &fp)) != 0)
    663 		return (error);
    664 	mp = ((struct vnode *)fp->f_data)->v_mount;
    665 	sp = &mp->mnt_stat;
    666 	if ((error = VFS_STATFS(mp, sp, p)) != 0)
    667 		goto out;
    668 	sp->f_flags = mp->mnt_flag & MNT_VISFLAGMASK;
    669 	error = copyout(sp, SCARG(uap, buf), sizeof(*sp));
    670  out:
    671 	FILE_UNUSE(fp, p);
    672 	return (error);
    673 }
    674 
    675 /*
    676  * Get statistics on all filesystems.
    677  */
    678 int
    679 sys_getfsstat(p, v, retval)
    680 	struct proc *p;
    681 	void *v;
    682 	register_t *retval;
    683 {
    684 	register struct sys_getfsstat_args /* {
    685 		syscallarg(struct statfs *) buf;
    686 		syscallarg(long) bufsize;
    687 		syscallarg(int) flags;
    688 	} */ *uap = v;
    689 	register struct mount *mp, *nmp;
    690 	register struct statfs *sp;
    691 	caddr_t sfsp;
    692 	long count, maxcount, error;
    693 
    694 	maxcount = SCARG(uap, bufsize) / sizeof(struct statfs);
    695 	sfsp = (caddr_t)SCARG(uap, buf);
    696 	simple_lock(&mountlist_slock);
    697 	count = 0;
    698 	for (mp = mountlist.cqh_first; mp != (void *)&mountlist; mp = nmp) {
    699 		if (vfs_busy(mp, LK_NOWAIT, &mountlist_slock)) {
    700 			nmp = mp->mnt_list.cqe_next;
    701 			continue;
    702 		}
    703 		if (sfsp && count < maxcount) {
    704 			sp = &mp->mnt_stat;
    705 			/*
    706 			 * If MNT_NOWAIT or MNT_LAZY is specified, do not
    707 			 * refresh the fsstat cache. MNT_WAIT or MNT_LAXY
    708 			 * overrides MNT_NOWAIT.
    709 			 */
    710 			if (SCARG(uap, flags) != MNT_NOWAIT &&
    711 			    SCARG(uap, flags) != MNT_LAZY &&
    712 			    (SCARG(uap, flags) == MNT_WAIT ||
    713 			     SCARG(uap, flags) == 0) &&
    714 			    (error = VFS_STATFS(mp, sp, p)) != 0) {
    715 				simple_lock(&mountlist_slock);
    716 				nmp = mp->mnt_list.cqe_next;
    717 				vfs_unbusy(mp);
    718 				continue;
    719 			}
    720 			sp->f_flags = mp->mnt_flag & MNT_VISFLAGMASK;
    721 			error = copyout(sp, sfsp, sizeof(*sp));
    722 			if (error) {
    723 				vfs_unbusy(mp);
    724 				return (error);
    725 			}
    726 			sfsp += sizeof(*sp);
    727 		}
    728 		count++;
    729 		simple_lock(&mountlist_slock);
    730 		nmp = mp->mnt_list.cqe_next;
    731 		vfs_unbusy(mp);
    732 	}
    733 	simple_unlock(&mountlist_slock);
    734 	if (sfsp && count > maxcount)
    735 		*retval = maxcount;
    736 	else
    737 		*retval = count;
    738 	return (0);
    739 }
    740 
    741 /*
    742  * Change current working directory to a given file descriptor.
    743  */
    744 /* ARGSUSED */
    745 int
    746 sys_fchdir(p, v, retval)
    747 	struct proc *p;
    748 	void *v;
    749 	register_t *retval;
    750 {
    751 	struct sys_fchdir_args /* {
    752 		syscallarg(int) fd;
    753 	} */ *uap = v;
    754 	struct filedesc *fdp = p->p_fd;
    755 	struct cwdinfo *cwdi = p->p_cwdi;
    756 	struct vnode *vp, *tdp;
    757 	struct mount *mp;
    758 	struct file *fp;
    759 	int error;
    760 
    761 	/* getvnode() will use the descriptor for us */
    762 	if ((error = getvnode(fdp, SCARG(uap, fd), &fp)) != 0)
    763 		return (error);
    764 	vp = (struct vnode *)fp->f_data;
    765 
    766 	VREF(vp);
    767 	vn_lock(vp,  LK_EXCLUSIVE | LK_RETRY);
    768 	if (vp->v_type != VDIR)
    769 		error = ENOTDIR;
    770 	else
    771 		error = VOP_ACCESS(vp, VEXEC, p->p_ucred, p);
    772 	while (!error && (mp = vp->v_mountedhere) != NULL) {
    773 		if (vfs_busy(mp, 0, 0))
    774 			continue;
    775 		error = VFS_ROOT(mp, &tdp);
    776 		vfs_unbusy(mp);
    777 		if (error)
    778 			break;
    779 		vput(vp);
    780 		vp = tdp;
    781 	}
    782 	if (error) {
    783 		vput(vp);
    784 		goto out;
    785 	}
    786 	VOP_UNLOCK(vp, 0);
    787 
    788 	/*
    789 	 * Disallow changing to a directory not under the process's
    790 	 * current root directory (if there is one).
    791 	 */
    792 	if (cwdi->cwdi_rdir && !vn_isunder(vp, NULL, p)) {
    793 		vrele(vp);
    794 		error = EPERM;	/* operation not permitted */
    795 		goto out;
    796 	}
    797 
    798 	vrele(cwdi->cwdi_cdir);
    799 	cwdi->cwdi_cdir = vp;
    800  out:
    801 	FILE_UNUSE(fp, p);
    802 	return (error);
    803 }
    804 
    805 /*
    806  * Change this process's notion of the root directory to a given file descriptor.
    807  */
    808 
    809 int
    810 sys_fchroot(p, v, retval)
    811 	struct proc *p;
    812 	void *v;
    813 	register_t *retval;
    814 {
    815 	struct sys_fchroot_args *uap = v;
    816 	struct filedesc *fdp = p->p_fd;
    817 	struct cwdinfo *cwdi = p->p_cwdi;
    818 	struct vnode	*vp;
    819 	struct file	*fp;
    820 	int		 error;
    821 
    822 	if ((error = suser(p->p_ucred, &p->p_acflag)) != 0)
    823 		return error;
    824 	/* getvnode() will use the descriptor for us */
    825 	if ((error = getvnode(fdp, SCARG(uap, fd), &fp)) != 0)
    826 		return error;
    827 	vp = (struct vnode *) fp->f_data;
    828 	vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
    829 	if (vp->v_type != VDIR)
    830 		error = ENOTDIR;
    831 	else
    832 		error = VOP_ACCESS(vp, VEXEC, p->p_ucred, p);
    833 	VOP_UNLOCK(vp, 0);
    834 	if (error)
    835 		goto out;
    836 	VREF(vp);
    837 
    838 	/*
    839 	 * Prevent escaping from chroot by putting the root under
    840 	 * the working directory.  Silently chdir to / if we aren't
    841 	 * already there.
    842 	 */
    843 	if (!vn_isunder(cwdi->cwdi_cdir, vp, p)) {
    844 		/*
    845 		 * XXX would be more failsafe to change directory to a
    846 		 * deadfs node here instead
    847 		 */
    848 		vrele(cwdi->cwdi_cdir);
    849 		VREF(vp);
    850 		cwdi->cwdi_cdir = vp;
    851 	}
    852 
    853 	if (cwdi->cwdi_rdir != NULL)
    854 		vrele(cwdi->cwdi_rdir);
    855 	cwdi->cwdi_rdir = vp;
    856  out:
    857 	FILE_UNUSE(fp, p);
    858 	return (error);
    859 }
    860 
    861 
    862 
    863 /*
    864  * Change current working directory (``.'').
    865  */
    866 /* ARGSUSED */
    867 int
    868 sys_chdir(p, v, retval)
    869 	struct proc *p;
    870 	void *v;
    871 	register_t *retval;
    872 {
    873 	struct sys_chdir_args /* {
    874 		syscallarg(const char *) path;
    875 	} */ *uap = v;
    876 	struct cwdinfo *cwdi = p->p_cwdi;
    877 	int error;
    878 	struct nameidata nd;
    879 
    880 	NDINIT(&nd, LOOKUP, FOLLOW | LOCKLEAF, UIO_USERSPACE,
    881 	    SCARG(uap, path), p);
    882 	if ((error = change_dir(&nd, p)) != 0)
    883 		return (error);
    884 	vrele(cwdi->cwdi_cdir);
    885 	cwdi->cwdi_cdir = nd.ni_vp;
    886 	return (0);
    887 }
    888 
    889 /*
    890  * Change notion of root (``/'') directory.
    891  */
    892 /* ARGSUSED */
    893 int
    894 sys_chroot(p, v, retval)
    895 	struct proc *p;
    896 	void *v;
    897 	register_t *retval;
    898 {
    899 	struct sys_chroot_args /* {
    900 		syscallarg(const char *) path;
    901 	} */ *uap = v;
    902 	struct cwdinfo *cwdi = p->p_cwdi;
    903 	struct vnode *vp;
    904 	int error;
    905 	struct nameidata nd;
    906 
    907 	if ((error = suser(p->p_ucred, &p->p_acflag)) != 0)
    908 		return (error);
    909 	NDINIT(&nd, LOOKUP, FOLLOW | LOCKLEAF, UIO_USERSPACE,
    910 	    SCARG(uap, path), p);
    911 	if ((error = change_dir(&nd, p)) != 0)
    912 		return (error);
    913 	if (cwdi->cwdi_rdir != NULL)
    914 		vrele(cwdi->cwdi_rdir);
    915 	vp = nd.ni_vp;
    916 	cwdi->cwdi_rdir = vp;
    917 
    918 	/*
    919 	 * Prevent escaping from chroot by putting the root under
    920 	 * the working directory.  Silently chdir to / if we aren't
    921 	 * already there.
    922 	 */
    923 	if (!vn_isunder(cwdi->cwdi_cdir, vp, p)) {
    924 		/*
    925 		 * XXX would be more failsafe to change directory to a
    926 		 * deadfs node here instead
    927 		 */
    928 		vrele(cwdi->cwdi_cdir);
    929 		VREF(vp);
    930 		cwdi->cwdi_cdir = vp;
    931 	}
    932 
    933 	return (0);
    934 }
    935 
    936 /*
    937  * Common routine for chroot and chdir.
    938  */
    939 static int
    940 change_dir(ndp, p)
    941 	register struct nameidata *ndp;
    942 	struct proc *p;
    943 {
    944 	struct vnode *vp;
    945 	int error;
    946 
    947 	if ((error = namei(ndp)) != 0)
    948 		return (error);
    949 	vp = ndp->ni_vp;
    950 	if (vp->v_type != VDIR)
    951 		error = ENOTDIR;
    952 	else
    953 		error = VOP_ACCESS(vp, VEXEC, p->p_ucred, p);
    954 
    955 	if (error)
    956 		vput(vp);
    957 	else
    958 		VOP_UNLOCK(vp, 0);
    959 	return (error);
    960 }
    961 
    962 /*
    963  * Check permissions, allocate an open file structure,
    964  * and call the device open routine if any.
    965  */
    966 int
    967 sys_open(p, v, retval)
    968 	struct proc *p;
    969 	void *v;
    970 	register_t *retval;
    971 {
    972 	register struct sys_open_args /* {
    973 		syscallarg(const char *) path;
    974 		syscallarg(int) flags;
    975 		syscallarg(int) mode;
    976 	} */ *uap = v;
    977 	struct cwdinfo *cwdi = p->p_cwdi;
    978 	struct filedesc *fdp = p->p_fd;
    979 	struct file *fp;
    980 	struct vnode *vp;
    981 	int flags, cmode;
    982 	int type, indx, error;
    983 	struct flock lf;
    984 	struct nameidata nd;
    985 	extern struct fileops vnops;
    986 
    987 	flags = FFLAGS(SCARG(uap, flags));
    988 	if ((flags & (FREAD | FWRITE)) == 0)
    989 		return (EINVAL);
    990 	/* falloc() will use the file descriptor for us */
    991 	if ((error = falloc(p, &fp, &indx)) != 0)
    992 		return (error);
    993 	cmode = ((SCARG(uap, mode) &~ cwdi->cwdi_cmask) & ALLPERMS) &~ S_ISTXT;
    994 	NDINIT(&nd, LOOKUP, FOLLOW, UIO_USERSPACE, SCARG(uap, path), p);
    995 	p->p_dupfd = -indx - 1;			/* XXX check for fdopen */
    996 	if ((error = vn_open(&nd, flags, cmode)) != 0) {
    997 		FILE_UNUSE(fp, p);
    998 		ffree(fp);
    999 		if ((error == ENODEV || error == ENXIO) &&
   1000 		    p->p_dupfd >= 0 &&			/* XXX from fdopen */
   1001 		    (error =
   1002 			dupfdopen(p, indx, p->p_dupfd, flags, error)) == 0) {
   1003 			*retval = indx;
   1004 			return (0);
   1005 		}
   1006 		if (error == ERESTART)
   1007 			error = EINTR;
   1008 		fdp->fd_ofiles[indx] = NULL;
   1009 		return (error);
   1010 	}
   1011 	p->p_dupfd = 0;
   1012 	vp = nd.ni_vp;
   1013 	fp->f_flag = flags & FMASK;
   1014 	fp->f_type = DTYPE_VNODE;
   1015 	fp->f_ops = &vnops;
   1016 	fp->f_data = (caddr_t)vp;
   1017 	if (flags & (O_EXLOCK | O_SHLOCK)) {
   1018 		lf.l_whence = SEEK_SET;
   1019 		lf.l_start = 0;
   1020 		lf.l_len = 0;
   1021 		if (flags & O_EXLOCK)
   1022 			lf.l_type = F_WRLCK;
   1023 		else
   1024 			lf.l_type = F_RDLCK;
   1025 		type = F_FLOCK;
   1026 		if ((flags & FNONBLOCK) == 0)
   1027 			type |= F_WAIT;
   1028 		VOP_UNLOCK(vp, 0);
   1029 		error = VOP_ADVLOCK(vp, (caddr_t)fp, F_SETLK, &lf, type);
   1030 		if (error) {
   1031 			(void) vn_close(vp, fp->f_flag, fp->f_cred, p);
   1032 			FILE_UNUSE(fp, p);
   1033 			ffree(fp);
   1034 			fdp->fd_ofiles[indx] = NULL;
   1035 			return (error);
   1036 		}
   1037 		vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
   1038 		fp->f_flag |= FHASLOCK;
   1039 	}
   1040 	VOP_UNLOCK(vp, 0);
   1041 	*retval = indx;
   1042 	FILE_UNUSE(fp, p);
   1043 	return (0);
   1044 }
   1045 
   1046 /*
   1047  * Get file handle system call
   1048  */
   1049 int
   1050 sys_getfh(p, v, retval)
   1051 	struct proc *p;
   1052 	register void *v;
   1053 	register_t *retval;
   1054 {
   1055 	register struct sys_getfh_args /* {
   1056 		syscallarg(char *) fname;
   1057 		syscallarg(fhandle_t *) fhp;
   1058 	} */ *uap = v;
   1059 	register struct vnode *vp;
   1060 	fhandle_t fh;
   1061 	int error;
   1062 	struct nameidata nd;
   1063 
   1064 	/*
   1065 	 * Must be super user
   1066 	 */
   1067 	error = suser(p->p_ucred, &p->p_acflag);
   1068 	if (error)
   1069 		return (error);
   1070 	NDINIT(&nd, LOOKUP, FOLLOW | LOCKLEAF, UIO_USERSPACE,
   1071 	    SCARG(uap, fname), p);
   1072 	error = namei(&nd);
   1073 	if (error)
   1074 		return (error);
   1075 	vp = nd.ni_vp;
   1076 	memset((caddr_t)&fh, 0, sizeof(fh));
   1077 	fh.fh_fsid = vp->v_mount->mnt_stat.f_fsid;
   1078 	error = VFS_VPTOFH(vp, &fh.fh_fid);
   1079 	vput(vp);
   1080 	if (error)
   1081 		return (error);
   1082 	error = copyout((caddr_t)&fh, (caddr_t)SCARG(uap, fhp), sizeof (fh));
   1083 	return (error);
   1084 }
   1085 
   1086 /*
   1087  * Open a file given a file handle.
   1088  *
   1089  * Check permissions, allocate an open file structure,
   1090  * and call the device open routine if any.
   1091  */
   1092 int
   1093 sys_fhopen(p, v, retval)
   1094 	struct proc *p;
   1095 	void *v;
   1096 	register_t *retval;
   1097 {
   1098 	register struct sys_fhopen_args /* {
   1099 		syscallarg(const fhandle_t *) fhp;
   1100 		syscallarg(int) flags;
   1101 	} */ *uap = v;
   1102 	struct filedesc *fdp = p->p_fd;
   1103 	struct file *fp;
   1104 	struct vnode *vp = NULL;
   1105 	struct mount *mp;
   1106 	struct ucred *cred = p->p_ucred;
   1107 	int flags;
   1108 	struct file *nfp;
   1109 	int type, indx, error=0;
   1110 	struct flock lf;
   1111 	struct vattr va;
   1112 	fhandle_t fh;
   1113 	extern struct fileops vnops;
   1114 
   1115 	/*
   1116 	 * Must be super user
   1117 	 */
   1118 	if ((error = suser(p->p_ucred, &p->p_acflag)))
   1119 		return (error);
   1120 
   1121 	flags = FFLAGS(SCARG(uap, flags));
   1122 	if ((flags & (FREAD | FWRITE)) == 0)
   1123 		return (EINVAL);
   1124 	if ((flags & O_CREAT))
   1125 		return (EINVAL);
   1126 	/* falloc() will use the file descriptor for us */
   1127 	if ((error = falloc(p, &nfp, &indx)) != 0)
   1128 		return (error);
   1129 	fp = nfp;
   1130 	if ((error = copyin(SCARG(uap, fhp), &fh, sizeof(fhandle_t))) != 0)
   1131 		goto bad;
   1132 
   1133 	if ((mp = vfs_getvfs(&fh.fh_fsid)) == NULL) {
   1134 		error = ESTALE;
   1135 		goto bad;
   1136 	}
   1137 
   1138 	if ((error = VFS_FHTOVP(mp, &fh.fh_fid, &vp)) != 0) {
   1139 		vp = NULL;	/* most likely unnecessary sanity for bad: */
   1140 		goto bad;
   1141 	}
   1142 
   1143 	/* Now do an effective vn_open */
   1144 
   1145 	if (vp->v_type == VSOCK) {
   1146 		error = EOPNOTSUPP;
   1147 		goto bad;
   1148 	}
   1149 	if (flags & FREAD) {
   1150 		if ((error = VOP_ACCESS(vp, VREAD, cred, p)) != 0)
   1151 			goto bad;
   1152 	}
   1153 	if (flags & (FWRITE | O_TRUNC)) {
   1154 		if (vp->v_type == VDIR) {
   1155 			error = EISDIR;
   1156 			goto bad;
   1157 		}
   1158 		if ((error = vn_writechk(vp)) != 0 ||
   1159 		    (error = VOP_ACCESS(vp, VWRITE, cred, p)) != 0)
   1160 			goto bad;
   1161 	}
   1162 	if (flags & O_TRUNC) {
   1163 		VOP_UNLOCK(vp, 0);			/* XXX */
   1164 		VOP_LEASE(vp, p, cred, LEASE_WRITE);
   1165 		vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);   /* XXX */
   1166 		VATTR_NULL(&va);
   1167 		va.va_size = 0;
   1168 		if ((error = VOP_SETATTR(vp, &va, cred, p)) != 0)
   1169 			goto bad;
   1170 	}
   1171 	if ((error = VOP_OPEN(vp, flags, cred, p)) != 0)
   1172 		goto bad;
   1173 	if (flags & FWRITE)
   1174 		vp->v_writecount++;
   1175 
   1176 	/* done with modified vn_open, now finish what sys_open does. */
   1177 
   1178 	fp->f_flag = flags & FMASK;
   1179 	fp->f_type = DTYPE_VNODE;
   1180 	fp->f_ops = &vnops;
   1181 	fp->f_data = (caddr_t)vp;
   1182 	if (flags & (O_EXLOCK | O_SHLOCK)) {
   1183 		lf.l_whence = SEEK_SET;
   1184 		lf.l_start = 0;
   1185 		lf.l_len = 0;
   1186 		if (flags & O_EXLOCK)
   1187 			lf.l_type = F_WRLCK;
   1188 		else
   1189 			lf.l_type = F_RDLCK;
   1190 		type = F_FLOCK;
   1191 		if ((flags & FNONBLOCK) == 0)
   1192 			type |= F_WAIT;
   1193 		VOP_UNLOCK(vp, 0);
   1194 		error = VOP_ADVLOCK(vp, (caddr_t)fp, F_SETLK, &lf, type);
   1195 		if (error) {
   1196 			(void) vn_close(vp, fp->f_flag, fp->f_cred, p);
   1197 			FILE_UNUSE(fp, p);
   1198 			ffree(fp);
   1199 			fdp->fd_ofiles[indx] = NULL;
   1200 			return (error);
   1201 		}
   1202 		vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
   1203 		fp->f_flag |= FHASLOCK;
   1204 	}
   1205 	VOP_UNLOCK(vp, 0);
   1206 	*retval = indx;
   1207 	FILE_UNUSE(fp, p);
   1208 	return (0);
   1209 
   1210 bad:
   1211 	FILE_UNUSE(fp, p);
   1212 	ffree(fp);
   1213 	fdp->fd_ofiles[indx] = NULL;
   1214 	if (vp != NULL)
   1215 		vput(vp);
   1216 	return (error);
   1217 }
   1218 
   1219 /* ARGSUSED */
   1220 int
   1221 sys_fhstat(p, v, retval)
   1222 	struct proc *p;
   1223 	void *v;
   1224 	register_t *retval;
   1225 {
   1226 	register struct sys_fhstat_args /* {
   1227 		syscallarg(const fhandle_t *) fhp;
   1228 		syscallarg(struct stat *) sb;
   1229 	} */ *uap = v;
   1230 	struct stat sb;
   1231 	int error;
   1232 	fhandle_t fh;
   1233 	struct mount *mp;
   1234 	struct vnode *vp;
   1235 
   1236 	/*
   1237 	 * Must be super user
   1238 	 */
   1239 	if ((error = suser(p->p_ucred, &p->p_acflag)))
   1240 		return (error);
   1241 
   1242 	if ((error = copyin(SCARG(uap, fhp), &fh, sizeof(fhandle_t))) != 0)
   1243 		return (error);
   1244 
   1245 	if ((mp = vfs_getvfs(&fh.fh_fsid)) == NULL)
   1246 		return (ESTALE);
   1247 	if ((error = VFS_FHTOVP(mp, &fh.fh_fid, &vp)))
   1248 		return (error);
   1249 	error = vn_stat(vp, &sb, p);
   1250 	vput(vp);
   1251 	if (error)
   1252 		return (error);
   1253 	error = copyout(&sb, SCARG(uap, sb), sizeof(sb));
   1254 	return (error);
   1255 }
   1256 
   1257 /* ARGSUSED */
   1258 int
   1259 sys_fhstatfs(p, v, retval)
   1260 	struct proc *p;
   1261 	void *v;
   1262 	register_t *retval;
   1263 {
   1264 	register struct sys_fhstatfs_args /*
   1265 		syscallarg(const fhandle_t *) fhp;
   1266 		syscallarg(struct statfs *) buf;
   1267 	} */ *uap = v;
   1268 	struct statfs sp;
   1269 	fhandle_t fh;
   1270 	struct mount *mp;
   1271 	struct vnode *vp;
   1272 	int error;
   1273 
   1274 	/*
   1275 	 * Must be super user
   1276 	 */
   1277 	if ((error = suser(p->p_ucred, &p->p_acflag)))
   1278 		return (error);
   1279 
   1280 	if ((error = copyin(SCARG(uap, fhp), &fh, sizeof(fhandle_t))) != 0)
   1281 		return (error);
   1282 
   1283 	if ((mp = vfs_getvfs(&fh.fh_fsid)) == NULL)
   1284 		return (ESTALE);
   1285 	if ((error = VFS_FHTOVP(mp, &fh.fh_fid, &vp)))
   1286 		return (error);
   1287 	mp = vp->v_mount;
   1288 	vput(vp);
   1289 	if ((error = VFS_STATFS(mp, &sp, p)) != 0)
   1290 		return (error);
   1291 	sp.f_flags = mp->mnt_flag & MNT_VISFLAGMASK;
   1292 	return (copyout(&sp, SCARG(uap, buf), sizeof(sp)));
   1293 }
   1294 
   1295 /*
   1296  * Create a special file.
   1297  */
   1298 /* ARGSUSED */
   1299 int
   1300 sys_mknod(p, v, retval)
   1301 	struct proc *p;
   1302 	void *v;
   1303 	register_t *retval;
   1304 {
   1305 	register struct sys_mknod_args /* {
   1306 		syscallarg(const char *) path;
   1307 		syscallarg(int) mode;
   1308 		syscallarg(int) dev;
   1309 	} */ *uap = v;
   1310 	register struct vnode *vp;
   1311 	struct vattr vattr;
   1312 	int error;
   1313 	int whiteout = 0;
   1314 	struct nameidata nd;
   1315 
   1316 	if ((error = suser(p->p_ucred, &p->p_acflag)) != 0)
   1317 		return (error);
   1318 	NDINIT(&nd, CREATE, LOCKPARENT, UIO_USERSPACE, SCARG(uap, path), p);
   1319 	if ((error = namei(&nd)) != 0)
   1320 		return (error);
   1321 	vp = nd.ni_vp;
   1322 	if (vp != NULL)
   1323 		error = EEXIST;
   1324 	else {
   1325 		VATTR_NULL(&vattr);
   1326 		vattr.va_mode =
   1327 		    (SCARG(uap, mode) & ALLPERMS) &~ p->p_cwdi->cwdi_cmask;
   1328 		vattr.va_rdev = SCARG(uap, dev);
   1329 		whiteout = 0;
   1330 
   1331 		switch (SCARG(uap, mode) & S_IFMT) {
   1332 		case S_IFMT:	/* used by badsect to flag bad sectors */
   1333 			vattr.va_type = VBAD;
   1334 			break;
   1335 		case S_IFCHR:
   1336 			vattr.va_type = VCHR;
   1337 			break;
   1338 		case S_IFBLK:
   1339 			vattr.va_type = VBLK;
   1340 			break;
   1341 		case S_IFWHT:
   1342 			whiteout = 1;
   1343 			break;
   1344 		default:
   1345 			error = EINVAL;
   1346 			break;
   1347 		}
   1348 	}
   1349 	if (!error) {
   1350 		VOP_LEASE(nd.ni_dvp, p, p->p_ucred, LEASE_WRITE);
   1351 		if (whiteout) {
   1352 			error = VOP_WHITEOUT(nd.ni_dvp, &nd.ni_cnd, CREATE);
   1353 			if (error)
   1354 				VOP_ABORTOP(nd.ni_dvp, &nd.ni_cnd);
   1355 			vput(nd.ni_dvp);
   1356 		} else {
   1357 			error = VOP_MKNOD(nd.ni_dvp, &nd.ni_vp,
   1358 						&nd.ni_cnd, &vattr);
   1359 		}
   1360 	} else {
   1361 		VOP_ABORTOP(nd.ni_dvp, &nd.ni_cnd);
   1362 		if (nd.ni_dvp == vp)
   1363 			vrele(nd.ni_dvp);
   1364 		else
   1365 			vput(nd.ni_dvp);
   1366 		if (vp)
   1367 			vrele(vp);
   1368 	}
   1369 	return (error);
   1370 }
   1371 
   1372 /*
   1373  * Create a named pipe.
   1374  */
   1375 /* ARGSUSED */
   1376 int
   1377 sys_mkfifo(p, v, retval)
   1378 	struct proc *p;
   1379 	void *v;
   1380 	register_t *retval;
   1381 {
   1382 	register struct sys_mkfifo_args /* {
   1383 		syscallarg(const char *) path;
   1384 		syscallarg(int) mode;
   1385 	} */ *uap = v;
   1386 	struct vattr vattr;
   1387 	int error;
   1388 	struct nameidata nd;
   1389 
   1390 	NDINIT(&nd, CREATE, LOCKPARENT, UIO_USERSPACE, SCARG(uap, path), p);
   1391 	if ((error = namei(&nd)) != 0)
   1392 		return (error);
   1393 	if (nd.ni_vp != NULL) {
   1394 		VOP_ABORTOP(nd.ni_dvp, &nd.ni_cnd);
   1395 		if (nd.ni_dvp == nd.ni_vp)
   1396 			vrele(nd.ni_dvp);
   1397 		else
   1398 			vput(nd.ni_dvp);
   1399 		vrele(nd.ni_vp);
   1400 		return (EEXIST);
   1401 	}
   1402 	VATTR_NULL(&vattr);
   1403 	vattr.va_type = VFIFO;
   1404 	vattr.va_mode = (SCARG(uap, mode) & ALLPERMS) &~ p->p_cwdi->cwdi_cmask;
   1405 	VOP_LEASE(nd.ni_dvp, p, p->p_ucred, LEASE_WRITE);
   1406 	return (VOP_MKNOD(nd.ni_dvp, &nd.ni_vp, &nd.ni_cnd, &vattr));
   1407 }
   1408 
   1409 /*
   1410  * Make a hard file link.
   1411  */
   1412 /* ARGSUSED */
   1413 int
   1414 sys_link(p, v, retval)
   1415 	struct proc *p;
   1416 	void *v;
   1417 	register_t *retval;
   1418 {
   1419 	register struct sys_link_args /* {
   1420 		syscallarg(const char *) path;
   1421 		syscallarg(const char *) link;
   1422 	} */ *uap = v;
   1423 	register struct vnode *vp;
   1424 	struct nameidata nd;
   1425 	int error;
   1426 
   1427 	NDINIT(&nd, LOOKUP, NOFOLLOW, UIO_USERSPACE, SCARG(uap, path), p);
   1428 	if ((error = namei(&nd)) != 0)
   1429 		return (error);
   1430 	vp = nd.ni_vp;
   1431 	NDINIT(&nd, CREATE, LOCKPARENT, UIO_USERSPACE, SCARG(uap, link), p);
   1432 	if ((error = namei(&nd)) != 0)
   1433 		goto out;
   1434 	if (nd.ni_vp) {
   1435 		VOP_ABORTOP(nd.ni_dvp, &nd.ni_cnd);
   1436 		if (nd.ni_dvp == nd.ni_vp)
   1437 			vrele(nd.ni_dvp);
   1438 		else
   1439 			vput(nd.ni_dvp);
   1440 		vrele(nd.ni_vp);
   1441 		error = EEXIST;
   1442 		goto out;
   1443 	}
   1444 	VOP_LEASE(nd.ni_dvp, p, p->p_ucred, LEASE_WRITE);
   1445 	VOP_LEASE(vp, p, p->p_ucred, LEASE_WRITE);
   1446 	error = VOP_LINK(nd.ni_dvp, vp, &nd.ni_cnd);
   1447 out:
   1448 	vrele(vp);
   1449 	return (error);
   1450 }
   1451 
   1452 /*
   1453  * Make a symbolic link.
   1454  */
   1455 /* ARGSUSED */
   1456 int
   1457 sys_symlink(p, v, retval)
   1458 	struct proc *p;
   1459 	void *v;
   1460 	register_t *retval;
   1461 {
   1462 	register struct sys_symlink_args /* {
   1463 		syscallarg(const char *) path;
   1464 		syscallarg(const char *) link;
   1465 	} */ *uap = v;
   1466 	struct vattr vattr;
   1467 	char *path;
   1468 	int error;
   1469 	struct nameidata nd;
   1470 
   1471 	MALLOC(path, char *, MAXPATHLEN, M_NAMEI, M_WAITOK);
   1472 	error = copyinstr(SCARG(uap, path), path, MAXPATHLEN, NULL);
   1473 	if (error)
   1474 		goto out;
   1475 	NDINIT(&nd, CREATE, LOCKPARENT, UIO_USERSPACE, SCARG(uap, link), p);
   1476 	if ((error = namei(&nd)) != 0)
   1477 		goto out;
   1478 	if (nd.ni_vp) {
   1479 		VOP_ABORTOP(nd.ni_dvp, &nd.ni_cnd);
   1480 		if (nd.ni_dvp == nd.ni_vp)
   1481 			vrele(nd.ni_dvp);
   1482 		else
   1483 			vput(nd.ni_dvp);
   1484 		vrele(nd.ni_vp);
   1485 		error = EEXIST;
   1486 		goto out;
   1487 	}
   1488 	VATTR_NULL(&vattr);
   1489 	vattr.va_mode = ACCESSPERMS &~ p->p_cwdi->cwdi_cmask;
   1490 	VOP_LEASE(nd.ni_dvp, p, p->p_ucred, LEASE_WRITE);
   1491 	error = VOP_SYMLINK(nd.ni_dvp, &nd.ni_vp, &nd.ni_cnd, &vattr, path);
   1492 out:
   1493 	FREE(path, M_NAMEI);
   1494 	return (error);
   1495 }
   1496 
   1497 /*
   1498  * Delete a whiteout from the filesystem.
   1499  */
   1500 /* ARGSUSED */
   1501 int
   1502 sys_undelete(p, v, retval)
   1503 	struct proc *p;
   1504 	void *v;
   1505 	register_t *retval;
   1506 {
   1507 	register struct sys_undelete_args /* {
   1508 		syscallarg(const char *) path;
   1509 	} */ *uap = v;
   1510 	int error;
   1511 	struct nameidata nd;
   1512 
   1513 	NDINIT(&nd, DELETE, LOCKPARENT|DOWHITEOUT, UIO_USERSPACE,
   1514 	    SCARG(uap, path), p);
   1515 	error = namei(&nd);
   1516 	if (error)
   1517 		return (error);
   1518 
   1519 	if (nd.ni_vp != NULLVP || !(nd.ni_cnd.cn_flags & ISWHITEOUT)) {
   1520 		VOP_ABORTOP(nd.ni_dvp, &nd.ni_cnd);
   1521 		if (nd.ni_dvp == nd.ni_vp)
   1522 			vrele(nd.ni_dvp);
   1523 		else
   1524 			vput(nd.ni_dvp);
   1525 		if (nd.ni_vp)
   1526 			vrele(nd.ni_vp);
   1527 		return (EEXIST);
   1528 	}
   1529 
   1530 	VOP_LEASE(nd.ni_dvp, p, p->p_ucred, LEASE_WRITE);
   1531 	if ((error = VOP_WHITEOUT(nd.ni_dvp, &nd.ni_cnd, DELETE)) != 0)
   1532 		VOP_ABORTOP(nd.ni_dvp, &nd.ni_cnd);
   1533 	vput(nd.ni_dvp);
   1534 	return (error);
   1535 }
   1536 
   1537 /*
   1538  * Delete a name from the filesystem.
   1539  */
   1540 /* ARGSUSED */
   1541 int
   1542 sys_unlink(p, v, retval)
   1543 	struct proc *p;
   1544 	void *v;
   1545 	register_t *retval;
   1546 {
   1547 	struct sys_unlink_args /* {
   1548 		syscallarg(const char *) path;
   1549 	} */ *uap = v;
   1550 	register struct vnode *vp;
   1551 	int error;
   1552 	struct nameidata nd;
   1553 
   1554 	NDINIT(&nd, DELETE, LOCKPARENT | LOCKLEAF, UIO_USERSPACE,
   1555 	    SCARG(uap, path), p);
   1556 	if ((error = namei(&nd)) != 0)
   1557 		return (error);
   1558 	vp = nd.ni_vp;
   1559 
   1560 	/*
   1561 	 * The root of a mounted filesystem cannot be deleted.
   1562 	 */
   1563 	if (vp->v_flag & VROOT) {
   1564 		VOP_ABORTOP(nd.ni_dvp, &nd.ni_cnd);
   1565 		if (nd.ni_dvp == vp)
   1566 			vrele(nd.ni_dvp);
   1567 		else
   1568 			vput(nd.ni_dvp);
   1569 		vput(vp);
   1570 		error = EBUSY;
   1571 		goto out;
   1572 	}
   1573 
   1574 	(void)uvm_vnp_uncache(vp);
   1575 
   1576 	VOP_LEASE(nd.ni_dvp, p, p->p_ucred, LEASE_WRITE);
   1577 	VOP_LEASE(vp, p, p->p_ucred, LEASE_WRITE);
   1578 	error = VOP_REMOVE(nd.ni_dvp, nd.ni_vp, &nd.ni_cnd);
   1579 out:
   1580 	return (error);
   1581 }
   1582 
   1583 /*
   1584  * Reposition read/write file offset.
   1585  */
   1586 int
   1587 sys_lseek(p, v, retval)
   1588 	struct proc *p;
   1589 	void *v;
   1590 	register_t *retval;
   1591 {
   1592 	register struct sys_lseek_args /* {
   1593 		syscallarg(int) fd;
   1594 		syscallarg(int) pad;
   1595 		syscallarg(off_t) offset;
   1596 		syscallarg(int) whence;
   1597 	} */ *uap = v;
   1598 	struct ucred *cred = p->p_ucred;
   1599 	register struct filedesc *fdp = p->p_fd;
   1600 	register struct file *fp;
   1601 	struct vnode *vp;
   1602 	struct vattr vattr;
   1603 	register off_t newoff;
   1604 	int error;
   1605 
   1606 	if ((u_int)SCARG(uap, fd) >= fdp->fd_nfiles ||
   1607 	    (fp = fdp->fd_ofiles[SCARG(uap, fd)]) == NULL ||
   1608 	    (fp->f_iflags & FIF_WANTCLOSE) != 0)
   1609 		return (EBADF);
   1610 
   1611 	FILE_USE(fp);
   1612 
   1613 	vp = (struct vnode *)fp->f_data;
   1614 	if (fp->f_type != DTYPE_VNODE || vp->v_type == VFIFO) {
   1615 		error = ESPIPE;
   1616 		goto out;
   1617 	}
   1618 
   1619 	switch (SCARG(uap, whence)) {
   1620 	case SEEK_CUR:
   1621 		newoff = fp->f_offset + SCARG(uap, offset);
   1622 		break;
   1623 	case SEEK_END:
   1624 		error = VOP_GETATTR(vp, &vattr, cred, p);
   1625 		if (error)
   1626 			goto out;
   1627 		newoff = SCARG(uap, offset) + vattr.va_size;
   1628 		break;
   1629 	case SEEK_SET:
   1630 		newoff = SCARG(uap, offset);
   1631 		break;
   1632 	default:
   1633 		error = EINVAL;
   1634 		goto out;
   1635 	}
   1636 	if ((error = VOP_SEEK(vp, fp->f_offset, newoff, cred)) != 0)
   1637 		goto out;
   1638 
   1639 	*(off_t *)retval = fp->f_offset = newoff;
   1640  out:
   1641 	FILE_UNUSE(fp, p);
   1642 	return (error);
   1643 }
   1644 
   1645 /*
   1646  * Positional read system call.
   1647  */
   1648 int
   1649 sys_pread(p, v, retval)
   1650 	struct proc *p;
   1651 	void *v;
   1652 	register_t *retval;
   1653 {
   1654 	struct sys_pread_args /* {
   1655 		syscallarg(int) fd;
   1656 		syscallarg(void *) buf;
   1657 		syscallarg(size_t) nbyte;
   1658 		syscallarg(off_t) offset;
   1659 	} */ *uap = v;
   1660 	struct filedesc *fdp = p->p_fd;
   1661 	struct file *fp;
   1662 	struct vnode *vp;
   1663 	off_t offset;
   1664 	int error, fd = SCARG(uap, fd);
   1665 
   1666 	if ((u_int)fd >= fdp->fd_nfiles ||
   1667 	    (fp = fdp->fd_ofiles[fd]) == NULL ||
   1668 	    (fp->f_iflags & FIF_WANTCLOSE) != 0 ||
   1669 	    (fp->f_flag & FREAD) == 0)
   1670 		return (EBADF);
   1671 
   1672 	FILE_USE(fp);
   1673 
   1674 	vp = (struct vnode *)fp->f_data;
   1675 	if (fp->f_type != DTYPE_VNODE || vp->v_type == VFIFO) {
   1676 		error = ESPIPE;
   1677 		goto out;
   1678 	}
   1679 
   1680 	offset = SCARG(uap, offset);
   1681 
   1682 	/*
   1683 	 * XXX This works because no file systems actually
   1684 	 * XXX take any action on the seek operation.
   1685 	 */
   1686 	if ((error = VOP_SEEK(vp, fp->f_offset, offset, fp->f_cred)) != 0)
   1687 		goto out;
   1688 
   1689 	/* dofileread() will unuse the descriptor for us */
   1690 	return (dofileread(p, fd, fp, SCARG(uap, buf), SCARG(uap, nbyte),
   1691 	    &offset, 0, retval));
   1692 
   1693  out:
   1694 	FILE_UNUSE(fp, p);
   1695 	return (error);
   1696 }
   1697 
   1698 /*
   1699  * Positional scatter read system call.
   1700  */
   1701 int
   1702 sys_preadv(p, v, retval)
   1703 	struct proc *p;
   1704 	void *v;
   1705 	register_t *retval;
   1706 {
   1707 	struct sys_preadv_args /* {
   1708 		syscallarg(int) fd;
   1709 		syscallarg(const struct iovec *) iovp;
   1710 		syscallarg(int) iovcnt;
   1711 		syscallarg(off_t) offset;
   1712 	} */ *uap = v;
   1713 	struct filedesc *fdp = p->p_fd;
   1714 	struct file *fp;
   1715 	struct vnode *vp;
   1716 	off_t offset;
   1717 	int error, fd = SCARG(uap, fd);
   1718 
   1719 	if ((u_int)fd >= fdp->fd_nfiles ||
   1720 	    (fp = fdp->fd_ofiles[fd]) == NULL ||
   1721 	    (fp->f_iflags & FIF_WANTCLOSE) != 0 ||
   1722 	    (fp->f_flag & FREAD) == 0)
   1723 		return (EBADF);
   1724 
   1725 	FILE_USE(fp);
   1726 
   1727 	vp = (struct vnode *)fp->f_data;
   1728 	if (fp->f_type != DTYPE_VNODE || vp->v_type == VFIFO) {
   1729 		error = ESPIPE;
   1730 		goto out;
   1731 	}
   1732 
   1733 	offset = SCARG(uap, offset);
   1734 
   1735 	/*
   1736 	 * XXX This works because no file systems actually
   1737 	 * XXX take any action on the seek operation.
   1738 	 */
   1739 	if ((error = VOP_SEEK(vp, fp->f_offset, offset, fp->f_cred)) != 0)
   1740 		goto out;
   1741 
   1742 	/* dofilereadv() will unuse the descriptor for us */
   1743 	return (dofilereadv(p, fd, fp, SCARG(uap, iovp), SCARG(uap, iovcnt),
   1744 	    &offset, 0, retval));
   1745 
   1746  out:
   1747 	FILE_UNUSE(fp, p);
   1748 	return (error);
   1749 }
   1750 
   1751 /*
   1752  * Positional write system call.
   1753  */
   1754 int
   1755 sys_pwrite(p, v, retval)
   1756 	struct proc *p;
   1757 	void *v;
   1758 	register_t *retval;
   1759 {
   1760 	struct sys_pwrite_args /* {
   1761 		syscallarg(int) fd;
   1762 		syscallarg(const void *) buf;
   1763 		syscallarg(size_t) nbyte;
   1764 		syscallarg(off_t) offset;
   1765 	} */ *uap = v;
   1766 	struct filedesc *fdp = p->p_fd;
   1767 	struct file *fp;
   1768 	struct vnode *vp;
   1769 	off_t offset;
   1770 	int error, fd = SCARG(uap, fd);
   1771 
   1772 	if ((u_int)fd >= fdp->fd_nfiles ||
   1773 	    (fp = fdp->fd_ofiles[fd]) == NULL ||
   1774 	    (fp->f_iflags & FIF_WANTCLOSE) != 0 ||
   1775 	    (fp->f_flag & FWRITE) == 0)
   1776 		return (EBADF);
   1777 
   1778 	FILE_USE(fp);
   1779 
   1780 	vp = (struct vnode *)fp->f_data;
   1781 	if (fp->f_type != DTYPE_VNODE || vp->v_type == VFIFO) {
   1782 		error = ESPIPE;
   1783 		goto out;
   1784 	}
   1785 
   1786 	offset = SCARG(uap, offset);
   1787 
   1788 	/*
   1789 	 * XXX This works because no file systems actually
   1790 	 * XXX take any action on the seek operation.
   1791 	 */
   1792 	if ((error = VOP_SEEK(vp, fp->f_offset, offset, fp->f_cred)) != 0)
   1793 		goto out;
   1794 
   1795 	/* dofilewrite() will unuse the descriptor for us */
   1796 	return (dofilewrite(p, fd, fp, SCARG(uap, buf), SCARG(uap, nbyte),
   1797 	    &offset, 0, retval));
   1798 
   1799  out:
   1800 	FILE_UNUSE(fp, p);
   1801 	return (error);
   1802 }
   1803 
   1804 /*
   1805  * Positional gather write system call.
   1806  */
   1807 int
   1808 sys_pwritev(p, v, retval)
   1809 	struct proc *p;
   1810 	void *v;
   1811 	register_t *retval;
   1812 {
   1813 	struct sys_pwritev_args /* {
   1814 		syscallarg(int) fd;
   1815 		syscallarg(const struct iovec *) iovp;
   1816 		syscallarg(int) iovcnt;
   1817 		syscallarg(off_t) offset;
   1818 	} */ *uap = v;
   1819 	struct filedesc *fdp = p->p_fd;
   1820 	struct file *fp;
   1821 	struct vnode *vp;
   1822 	off_t offset;
   1823 	int error, fd = SCARG(uap, fd);
   1824 
   1825 	if ((u_int)fd >= fdp->fd_nfiles ||
   1826 	    (fp = fdp->fd_ofiles[fd]) == NULL ||
   1827 	    (fp->f_iflags & FIF_WANTCLOSE) != 0 ||
   1828 	    (fp->f_flag & FWRITE) == 0)
   1829 		return (EBADF);
   1830 
   1831 	FILE_USE(fp);
   1832 
   1833 	vp = (struct vnode *)fp->f_data;
   1834 	if (fp->f_type != DTYPE_VNODE || vp->v_type == VFIFO) {
   1835 		error = ESPIPE;
   1836 		goto out;
   1837 	}
   1838 
   1839 	offset = SCARG(uap, offset);
   1840 
   1841 	/*
   1842 	 * XXX This works because no file systems actually
   1843 	 * XXX take any action on the seek operation.
   1844 	 */
   1845 	if ((error = VOP_SEEK(vp, fp->f_offset, offset, fp->f_cred)) != 0)
   1846 		goto out;
   1847 
   1848 	/* dofilewritev() will unuse the descriptor for us */
   1849 	return (dofilewritev(p, fd, fp, SCARG(uap, iovp), SCARG(uap, iovcnt),
   1850 	    &offset, 0, retval));
   1851 
   1852  out:
   1853 	FILE_UNUSE(fp, p);
   1854 	return (error);
   1855 }
   1856 
   1857 /*
   1858  * Check access permissions.
   1859  */
   1860 int
   1861 sys_access(p, v, retval)
   1862 	struct proc *p;
   1863 	void *v;
   1864 	register_t *retval;
   1865 {
   1866 	register struct sys_access_args /* {
   1867 		syscallarg(const char *) path;
   1868 		syscallarg(int) flags;
   1869 	} */ *uap = v;
   1870 	register struct ucred *cred = p->p_ucred;
   1871 	register struct vnode *vp;
   1872 	int error, flags, t_gid, t_uid;
   1873 	struct nameidata nd;
   1874 
   1875 	t_uid = cred->cr_uid;
   1876 	t_gid = cred->cr_gid;
   1877 	cred->cr_uid = p->p_cred->p_ruid;
   1878 	cred->cr_gid = p->p_cred->p_rgid;
   1879 	NDINIT(&nd, LOOKUP, FOLLOW | LOCKLEAF, UIO_USERSPACE,
   1880 	    SCARG(uap, path), p);
   1881 	if ((error = namei(&nd)) != 0)
   1882 		goto out1;
   1883 	vp = nd.ni_vp;
   1884 
   1885 	/* Flags == 0 means only check for existence. */
   1886 	if (SCARG(uap, flags)) {
   1887 		flags = 0;
   1888 		if (SCARG(uap, flags) & R_OK)
   1889 			flags |= VREAD;
   1890 		if (SCARG(uap, flags) & W_OK)
   1891 			flags |= VWRITE;
   1892 		if (SCARG(uap, flags) & X_OK)
   1893 			flags |= VEXEC;
   1894 
   1895 		error = VOP_ACCESS(vp, flags, cred, p);
   1896 		if (!error && (flags & VWRITE))
   1897 			error = vn_writechk(vp);
   1898 	}
   1899 	vput(vp);
   1900 out1:
   1901 	cred->cr_uid = t_uid;
   1902 	cred->cr_gid = t_gid;
   1903 	return (error);
   1904 }
   1905 
   1906 /*
   1907  * Get file status; this version follows links.
   1908  */
   1909 /* ARGSUSED */
   1910 int
   1911 sys___stat13(p, v, retval)
   1912 	struct proc *p;
   1913 	void *v;
   1914 	register_t *retval;
   1915 {
   1916 	register struct sys___stat13_args /* {
   1917 		syscallarg(const char *) path;
   1918 		syscallarg(struct stat *) ub;
   1919 	} */ *uap = v;
   1920 	struct stat sb;
   1921 	int error;
   1922 	struct nameidata nd;
   1923 
   1924 	NDINIT(&nd, LOOKUP, FOLLOW | LOCKLEAF, UIO_USERSPACE,
   1925 	    SCARG(uap, path), p);
   1926 	if ((error = namei(&nd)) != 0)
   1927 		return (error);
   1928 	error = vn_stat(nd.ni_vp, &sb, p);
   1929 	vput(nd.ni_vp);
   1930 	if (error)
   1931 		return (error);
   1932 	error = copyout(&sb, SCARG(uap, ub), sizeof(sb));
   1933 	return (error);
   1934 }
   1935 
   1936 /*
   1937  * Get file status; this version does not follow links.
   1938  */
   1939 /* ARGSUSED */
   1940 int
   1941 sys___lstat13(p, v, retval)
   1942 	struct proc *p;
   1943 	void *v;
   1944 	register_t *retval;
   1945 {
   1946 	register struct sys___lstat13_args /* {
   1947 		syscallarg(const char *) path;
   1948 		syscallarg(struct stat *) ub;
   1949 	} */ *uap = v;
   1950 	struct stat sb;
   1951 	int error;
   1952 	struct nameidata nd;
   1953 
   1954 	NDINIT(&nd, LOOKUP, NOFOLLOW | LOCKLEAF, UIO_USERSPACE,
   1955 	    SCARG(uap, path), p);
   1956 	if ((error = namei(&nd)) != 0)
   1957 		return (error);
   1958 	error = vn_stat(nd.ni_vp, &sb, p);
   1959 	vput(nd.ni_vp);
   1960 	if (error)
   1961 		return (error);
   1962 	error = copyout(&sb, SCARG(uap, ub), sizeof(sb));
   1963 	return (error);
   1964 }
   1965 
   1966 /*
   1967  * Get configurable pathname variables.
   1968  */
   1969 /* ARGSUSED */
   1970 int
   1971 sys_pathconf(p, v, retval)
   1972 	struct proc *p;
   1973 	void *v;
   1974 	register_t *retval;
   1975 {
   1976 	register struct sys_pathconf_args /* {
   1977 		syscallarg(const char *) path;
   1978 		syscallarg(int) name;
   1979 	} */ *uap = v;
   1980 	int error;
   1981 	struct nameidata nd;
   1982 
   1983 	NDINIT(&nd, LOOKUP, FOLLOW | LOCKLEAF, UIO_USERSPACE,
   1984 	    SCARG(uap, path), p);
   1985 	if ((error = namei(&nd)) != 0)
   1986 		return (error);
   1987 	error = VOP_PATHCONF(nd.ni_vp, SCARG(uap, name), retval);
   1988 	vput(nd.ni_vp);
   1989 	return (error);
   1990 }
   1991 
   1992 /*
   1993  * Return target name of a symbolic link.
   1994  */
   1995 /* ARGSUSED */
   1996 int
   1997 sys_readlink(p, v, retval)
   1998 	struct proc *p;
   1999 	void *v;
   2000 	register_t *retval;
   2001 {
   2002 	register struct sys_readlink_args /* {
   2003 		syscallarg(const char *) path;
   2004 		syscallarg(char *) buf;
   2005 		syscallarg(size_t) count;
   2006 	} */ *uap = v;
   2007 	register struct vnode *vp;
   2008 	struct iovec aiov;
   2009 	struct uio auio;
   2010 	int error;
   2011 	struct nameidata nd;
   2012 
   2013 	NDINIT(&nd, LOOKUP, NOFOLLOW | LOCKLEAF, UIO_USERSPACE,
   2014 	    SCARG(uap, path), p);
   2015 	if ((error = namei(&nd)) != 0)
   2016 		return (error);
   2017 	vp = nd.ni_vp;
   2018 	if (vp->v_type != VLNK)
   2019 		error = EINVAL;
   2020 	else if (!(vp->v_mount->mnt_flag & MNT_SYMPERM) ||
   2021 	    (error = VOP_ACCESS(vp, VREAD, p->p_ucred, p)) == 0) {
   2022 		aiov.iov_base = SCARG(uap, buf);
   2023 		aiov.iov_len = SCARG(uap, count);
   2024 		auio.uio_iov = &aiov;
   2025 		auio.uio_iovcnt = 1;
   2026 		auio.uio_offset = 0;
   2027 		auio.uio_rw = UIO_READ;
   2028 		auio.uio_segflg = UIO_USERSPACE;
   2029 		auio.uio_procp = p;
   2030 		auio.uio_resid = SCARG(uap, count);
   2031 		error = VOP_READLINK(vp, &auio, p->p_ucred);
   2032 	}
   2033 	vput(vp);
   2034 	*retval = SCARG(uap, count) - auio.uio_resid;
   2035 	return (error);
   2036 }
   2037 
   2038 /*
   2039  * Change flags of a file given a path name.
   2040  */
   2041 /* ARGSUSED */
   2042 int
   2043 sys_chflags(p, v, retval)
   2044 	struct proc *p;
   2045 	void *v;
   2046 	register_t *retval;
   2047 {
   2048 	register struct sys_chflags_args /* {
   2049 		syscallarg(const char *) path;
   2050 		syscallarg(u_long) flags;
   2051 	} */ *uap = v;
   2052 	register struct vnode *vp;
   2053 	struct vattr vattr;
   2054 	int error;
   2055 	struct nameidata nd;
   2056 
   2057 	NDINIT(&nd, LOOKUP, FOLLOW, UIO_USERSPACE, SCARG(uap, path), p);
   2058 	if ((error = namei(&nd)) != 0)
   2059 		return (error);
   2060 	vp = nd.ni_vp;
   2061 	VOP_LEASE(vp, p, p->p_ucred, LEASE_WRITE);
   2062 	vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
   2063 	/* Non-superusers cannot change the flags on devices, even if they
   2064 	   own them. */
   2065 	if (suser(p->p_ucred, &p->p_acflag)) {
   2066 		if ((error = VOP_GETATTR(vp, &vattr, p->p_ucred, p)) != 0)
   2067 			goto out;
   2068 		if (vattr.va_type == VCHR || vattr.va_type == VBLK) {
   2069 			error = EINVAL;
   2070 			goto out;
   2071 		}
   2072 	}
   2073 	VATTR_NULL(&vattr);
   2074 	vattr.va_flags = SCARG(uap, flags);
   2075 	error = VOP_SETATTR(vp, &vattr, p->p_ucred, p);
   2076 out:
   2077 	vput(vp);
   2078 	return (error);
   2079 }
   2080 
   2081 /*
   2082  * Change flags of a file given a file descriptor.
   2083  */
   2084 /* ARGSUSED */
   2085 int
   2086 sys_fchflags(p, v, retval)
   2087 	struct proc *p;
   2088 	void *v;
   2089 	register_t *retval;
   2090 {
   2091 	register struct sys_fchflags_args /* {
   2092 		syscallarg(int) fd;
   2093 		syscallarg(u_long) flags;
   2094 	} */ *uap = v;
   2095 	struct vattr vattr;
   2096 	struct vnode *vp;
   2097 	struct file *fp;
   2098 	int error;
   2099 
   2100 	/* getvnode() will use the descriptor for us */
   2101 	if ((error = getvnode(p->p_fd, SCARG(uap, fd), &fp)) != 0)
   2102 		return (error);
   2103 	vp = (struct vnode *)fp->f_data;
   2104 	VOP_LEASE(vp, p, p->p_ucred, LEASE_WRITE);
   2105 	vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
   2106 	/* Non-superusers cannot change the flags on devices, even if they
   2107 	   own them. */
   2108 	if (suser(p->p_ucred, &p->p_acflag)) {
   2109 		if ((error = VOP_GETATTR(vp, &vattr, p->p_ucred, p))
   2110 		    != 0)
   2111 			goto out;
   2112 		if (vattr.va_type == VCHR || vattr.va_type == VBLK) {
   2113 			error = EINVAL;
   2114 			goto out;
   2115 		}
   2116 	}
   2117 	VATTR_NULL(&vattr);
   2118 	vattr.va_flags = SCARG(uap, flags);
   2119 	error = VOP_SETATTR(vp, &vattr, p->p_ucred, p);
   2120 out:
   2121 	VOP_UNLOCK(vp, 0);
   2122 	FILE_UNUSE(fp, p);
   2123 	return (error);
   2124 }
   2125 
   2126 /*
   2127  * Change mode of a file given path name; this version follows links.
   2128  */
   2129 /* ARGSUSED */
   2130 int
   2131 sys_chmod(p, v, retval)
   2132 	struct proc *p;
   2133 	void *v;
   2134 	register_t *retval;
   2135 {
   2136 	register struct sys_chmod_args /* {
   2137 		syscallarg(const char *) path;
   2138 		syscallarg(int) mode;
   2139 	} */ *uap = v;
   2140 	int error;
   2141 	struct nameidata nd;
   2142 
   2143 	NDINIT(&nd, LOOKUP, FOLLOW, UIO_USERSPACE, SCARG(uap, path), p);
   2144 	if ((error = namei(&nd)) != 0)
   2145 		return (error);
   2146 
   2147 	error = change_mode(nd.ni_vp, SCARG(uap, mode), p);
   2148 
   2149 	vrele(nd.ni_vp);
   2150 	return (error);
   2151 }
   2152 
   2153 /*
   2154  * Change mode of a file given a file descriptor.
   2155  */
   2156 /* ARGSUSED */
   2157 int
   2158 sys_fchmod(p, v, retval)
   2159 	struct proc *p;
   2160 	void *v;
   2161 	register_t *retval;
   2162 {
   2163 	register struct sys_fchmod_args /* {
   2164 		syscallarg(int) fd;
   2165 		syscallarg(int) mode;
   2166 	} */ *uap = v;
   2167 	struct file *fp;
   2168 	int error;
   2169 
   2170 	/* getvnode() will use the descriptor for us */
   2171 	if ((error = getvnode(p->p_fd, SCARG(uap, fd), &fp)) != 0)
   2172 		return (error);
   2173 
   2174 	error = change_mode((struct vnode *)fp->f_data, SCARG(uap, mode), p);
   2175 	FILE_UNUSE(fp, p);
   2176 	return (error);
   2177 }
   2178 
   2179 /*
   2180  * Change mode of a file given path name; this version does not follow links.
   2181  */
   2182 /* ARGSUSED */
   2183 int
   2184 sys_lchmod(p, v, retval)
   2185 	struct proc *p;
   2186 	void *v;
   2187 	register_t *retval;
   2188 {
   2189 	register struct sys_lchmod_args /* {
   2190 		syscallarg(const char *) path;
   2191 		syscallarg(int) mode;
   2192 	} */ *uap = v;
   2193 	int error;
   2194 	struct nameidata nd;
   2195 
   2196 	NDINIT(&nd, LOOKUP, NOFOLLOW, UIO_USERSPACE, SCARG(uap, path), p);
   2197 	if ((error = namei(&nd)) != 0)
   2198 		return (error);
   2199 
   2200 	error = change_mode(nd.ni_vp, SCARG(uap, mode), p);
   2201 
   2202 	vrele(nd.ni_vp);
   2203 	return (error);
   2204 }
   2205 
   2206 /*
   2207  * Common routine to set mode given a vnode.
   2208  */
   2209 static int
   2210 change_mode(vp, mode, p)
   2211 	struct vnode *vp;
   2212 	int mode;
   2213 	struct proc *p;
   2214 {
   2215 	struct vattr vattr;
   2216 	int error;
   2217 
   2218 	VOP_LEASE(vp, p, p->p_ucred, LEASE_WRITE);
   2219 	vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
   2220 	VATTR_NULL(&vattr);
   2221 	vattr.va_mode = mode & ALLPERMS;
   2222 	error = VOP_SETATTR(vp, &vattr, p->p_ucred, p);
   2223 	VOP_UNLOCK(vp, 0);
   2224 	return (error);
   2225 }
   2226 
   2227 /*
   2228  * Set ownership given a path name; this version follows links.
   2229  */
   2230 /* ARGSUSED */
   2231 int
   2232 sys_chown(p, v, retval)
   2233 	struct proc *p;
   2234 	void *v;
   2235 	register_t *retval;
   2236 {
   2237 	register struct sys_chown_args /* {
   2238 		syscallarg(const char *) path;
   2239 		syscallarg(uid_t) uid;
   2240 		syscallarg(gid_t) gid;
   2241 	} */ *uap = v;
   2242 	int error;
   2243 	struct nameidata nd;
   2244 
   2245 	NDINIT(&nd, LOOKUP, FOLLOW, UIO_USERSPACE, SCARG(uap, path), p);
   2246 	if ((error = namei(&nd)) != 0)
   2247 		return (error);
   2248 
   2249 	error = change_owner(nd.ni_vp, SCARG(uap, uid), SCARG(uap, gid), p, 0);
   2250 
   2251 	vrele(nd.ni_vp);
   2252 	return (error);
   2253 }
   2254 
   2255 /*
   2256  * Set ownership given a path name; this version follows links.
   2257  * Provides POSIX semantics.
   2258  */
   2259 /* ARGSUSED */
   2260 int
   2261 sys___posix_chown(p, v, retval)
   2262 	struct proc *p;
   2263 	void *v;
   2264 	register_t *retval;
   2265 {
   2266 	register struct sys_chown_args /* {
   2267 		syscallarg(const char *) path;
   2268 		syscallarg(uid_t) uid;
   2269 		syscallarg(gid_t) gid;
   2270 	} */ *uap = v;
   2271 	int error;
   2272 	struct nameidata nd;
   2273 
   2274 	NDINIT(&nd, LOOKUP, FOLLOW, UIO_USERSPACE, SCARG(uap, path), p);
   2275 	if ((error = namei(&nd)) != 0)
   2276 		return (error);
   2277 
   2278 	error = change_owner(nd.ni_vp, SCARG(uap, uid), SCARG(uap, gid), p, 1);
   2279 
   2280 	vrele(nd.ni_vp);
   2281 	return (error);
   2282 }
   2283 
   2284 /*
   2285  * Set ownership given a file descriptor.
   2286  */
   2287 /* ARGSUSED */
   2288 int
   2289 sys_fchown(p, v, retval)
   2290 	struct proc *p;
   2291 	void *v;
   2292 	register_t *retval;
   2293 {
   2294 	register struct sys_fchown_args /* {
   2295 		syscallarg(int) fd;
   2296 		syscallarg(uid_t) uid;
   2297 		syscallarg(gid_t) gid;
   2298 	} */ *uap = v;
   2299 	int error;
   2300 	struct file *fp;
   2301 
   2302 	/* getvnode() will use the descriptor for us */
   2303 	if ((error = getvnode(p->p_fd, SCARG(uap, fd), &fp)) != 0)
   2304 		return (error);
   2305 
   2306 	error = change_owner((struct vnode *)fp->f_data, SCARG(uap, uid),
   2307 	    SCARG(uap, gid), p, 0);
   2308 	FILE_UNUSE(fp, p);
   2309 	return (error);
   2310 }
   2311 
   2312 /*
   2313  * Set ownership given a file descriptor, providing POSIX/XPG semantics.
   2314  */
   2315 /* ARGSUSED */
   2316 int
   2317 sys___posix_fchown(p, v, retval)
   2318 	struct proc *p;
   2319 	void *v;
   2320 	register_t *retval;
   2321 {
   2322 	register struct sys_fchown_args /* {
   2323 		syscallarg(int) fd;
   2324 		syscallarg(uid_t) uid;
   2325 		syscallarg(gid_t) gid;
   2326 	} */ *uap = v;
   2327 	int error;
   2328 	struct file *fp;
   2329 
   2330 	/* getvnode() will use the descriptor for us */
   2331 	if ((error = getvnode(p->p_fd, SCARG(uap, fd), &fp)) != 0)
   2332 		return (error);
   2333 
   2334 	error = change_owner((struct vnode *)fp->f_data, SCARG(uap, uid),
   2335 	    SCARG(uap, gid), p, 1);
   2336 	FILE_UNUSE(fp, p);
   2337 	return (error);
   2338 }
   2339 
   2340 /*
   2341  * Set ownership given a path name; this version does not follow links.
   2342  */
   2343 /* ARGSUSED */
   2344 int
   2345 sys_lchown(p, v, retval)
   2346 	struct proc *p;
   2347 	void *v;
   2348 	register_t *retval;
   2349 {
   2350 	register struct sys_lchown_args /* {
   2351 		syscallarg(const char *) path;
   2352 		syscallarg(uid_t) uid;
   2353 		syscallarg(gid_t) gid;
   2354 	} */ *uap = v;
   2355 	int error;
   2356 	struct nameidata nd;
   2357 
   2358 	NDINIT(&nd, LOOKUP, NOFOLLOW, UIO_USERSPACE, SCARG(uap, path), p);
   2359 	if ((error = namei(&nd)) != 0)
   2360 		return (error);
   2361 
   2362 	error = change_owner(nd.ni_vp, SCARG(uap, uid), SCARG(uap, gid), p, 0);
   2363 
   2364 	vrele(nd.ni_vp);
   2365 	return (error);
   2366 }
   2367 
   2368 /*
   2369  * Set ownership given a path name; this version does not follow links.
   2370  * Provides POSIX/XPG semantics.
   2371  */
   2372 /* ARGSUSED */
   2373 int
   2374 sys___posix_lchown(p, v, retval)
   2375 	struct proc *p;
   2376 	void *v;
   2377 	register_t *retval;
   2378 {
   2379 	register struct sys_lchown_args /* {
   2380 		syscallarg(const char *) path;
   2381 		syscallarg(uid_t) uid;
   2382 		syscallarg(gid_t) gid;
   2383 	} */ *uap = v;
   2384 	int error;
   2385 	struct nameidata nd;
   2386 
   2387 	NDINIT(&nd, LOOKUP, NOFOLLOW, UIO_USERSPACE, SCARG(uap, path), p);
   2388 	if ((error = namei(&nd)) != 0)
   2389 		return (error);
   2390 
   2391 	error = change_owner(nd.ni_vp, SCARG(uap, uid), SCARG(uap, gid), p, 1);
   2392 
   2393 	vrele(nd.ni_vp);
   2394 	return (error);
   2395 }
   2396 
   2397 /*
   2398  * Common routine to set ownership given a vnode.
   2399  */
   2400 static int
   2401 change_owner(vp, uid, gid, p, posix_semantics)
   2402 	register struct vnode *vp;
   2403 	uid_t uid;
   2404 	gid_t gid;
   2405 	struct proc *p;
   2406 	int posix_semantics;
   2407 {
   2408 	struct vattr vattr;
   2409 	mode_t newmode;
   2410 	int error;
   2411 
   2412 	VOP_LEASE(vp, p, p->p_ucred, LEASE_WRITE);
   2413 	vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
   2414 	if ((error = VOP_GETATTR(vp, &vattr, p->p_ucred, p)) != 0)
   2415 		goto out;
   2416 
   2417 #define CHANGED(x) ((x) != -1)
   2418 	newmode = vattr.va_mode;
   2419 	if (posix_semantics) {
   2420 		/*
   2421 		 * POSIX/XPG semantics: if the caller is not the super-user,
   2422 		 * clear set-user-id and set-group-id bits.  Both POSIX and
   2423 		 * the XPG consider the behaviour for calls by the super-user
   2424 		 * implementation-defined; we leave the set-user-id and set-
   2425 		 * group-id settings intact in that case.
   2426 		 */
   2427 		if (suser(p->p_ucred, NULL) != 0)
   2428 			newmode &= ~(S_ISUID | S_ISGID);
   2429 	} else {
   2430 		/*
   2431 		 * NetBSD semantics: when changing owner and/or group,
   2432 		 * clear the respective bit(s).
   2433 		 */
   2434 		if (CHANGED(uid))
   2435 			newmode &= ~S_ISUID;
   2436 		if (CHANGED(gid))
   2437 			newmode &= ~S_ISGID;
   2438 	}
   2439 	/* Update va_mode iff altered. */
   2440 	if (vattr.va_mode == newmode)
   2441 		newmode = VNOVAL;
   2442 
   2443 	VATTR_NULL(&vattr);
   2444 	vattr.va_uid = CHANGED(uid) ? uid : VNOVAL;
   2445 	vattr.va_gid = CHANGED(gid) ? gid : VNOVAL;
   2446 	vattr.va_mode = newmode;
   2447 	error = VOP_SETATTR(vp, &vattr, p->p_ucred, p);
   2448 #undef CHANGED
   2449 
   2450 out:
   2451 	VOP_UNLOCK(vp, 0);
   2452 	return (error);
   2453 }
   2454 
   2455 /*
   2456  * Set the access and modification times given a path name; this
   2457  * version follows links.
   2458  */
   2459 /* ARGSUSED */
   2460 int
   2461 sys_utimes(p, v, retval)
   2462 	struct proc *p;
   2463 	void *v;
   2464 	register_t *retval;
   2465 {
   2466 	register struct sys_utimes_args /* {
   2467 		syscallarg(const char *) path;
   2468 		syscallarg(const struct timeval *) tptr;
   2469 	} */ *uap = v;
   2470 	int error;
   2471 	struct nameidata nd;
   2472 
   2473 	NDINIT(&nd, LOOKUP, FOLLOW, UIO_USERSPACE, SCARG(uap, path), p);
   2474 	if ((error = namei(&nd)) != 0)
   2475 		return (error);
   2476 
   2477 	error = change_utimes(nd.ni_vp, SCARG(uap, tptr), p);
   2478 
   2479 	vrele(nd.ni_vp);
   2480 	return (error);
   2481 }
   2482 
   2483 /*
   2484  * Set the access and modification times given a file descriptor.
   2485  */
   2486 /* ARGSUSED */
   2487 int
   2488 sys_futimes(p, v, retval)
   2489 	struct proc *p;
   2490 	void *v;
   2491 	register_t *retval;
   2492 {
   2493 	register struct sys_futimes_args /* {
   2494 		syscallarg(int) fd;
   2495 		syscallarg(const struct timeval *) tptr;
   2496 	} */ *uap = v;
   2497 	int error;
   2498 	struct file *fp;
   2499 
   2500 	/* getvnode() will use the descriptor for us */
   2501 	if ((error = getvnode(p->p_fd, SCARG(uap, fd), &fp)) != 0)
   2502 		return (error);
   2503 
   2504 	error = change_utimes((struct vnode *)fp->f_data, SCARG(uap, tptr), p);
   2505 	FILE_UNUSE(fp, p);
   2506 	return (error);
   2507 }
   2508 
   2509 /*
   2510  * Set the access and modification times given a path name; this
   2511  * version does not follow links.
   2512  */
   2513 /* ARGSUSED */
   2514 int
   2515 sys_lutimes(p, v, retval)
   2516 	struct proc *p;
   2517 	void *v;
   2518 	register_t *retval;
   2519 {
   2520 	register struct sys_lutimes_args /* {
   2521 		syscallarg(const char *) path;
   2522 		syscallarg(const struct timeval *) tptr;
   2523 	} */ *uap = v;
   2524 	int error;
   2525 	struct nameidata nd;
   2526 
   2527 	NDINIT(&nd, LOOKUP, NOFOLLOW, UIO_USERSPACE, SCARG(uap, path), p);
   2528 	if ((error = namei(&nd)) != 0)
   2529 		return (error);
   2530 
   2531 	error = change_utimes(nd.ni_vp, SCARG(uap, tptr), p);
   2532 
   2533 	vrele(nd.ni_vp);
   2534 	return (error);
   2535 }
   2536 
   2537 /*
   2538  * Common routine to set access and modification times given a vnode.
   2539  */
   2540 static int
   2541 change_utimes(vp, tptr, p)
   2542 	struct vnode *vp;
   2543 	const struct timeval *tptr;
   2544 	struct proc *p;
   2545 {
   2546 	struct timeval tv[2];
   2547 	struct vattr vattr;
   2548 	int error;
   2549 
   2550 	VATTR_NULL(&vattr);
   2551 	if (tptr == NULL) {
   2552 		microtime(&tv[0]);
   2553 		tv[1] = tv[0];
   2554 		vattr.va_vaflags |= VA_UTIMES_NULL;
   2555 	} else {
   2556 		error = copyin(tptr, tv, sizeof(tv));
   2557 		if (error)
   2558 			return (error);
   2559 	}
   2560 	VOP_LEASE(vp, p, p->p_ucred, LEASE_WRITE);
   2561 	vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
   2562 	vattr.va_atime.tv_sec = tv[0].tv_sec;
   2563 	vattr.va_atime.tv_nsec = tv[0].tv_usec * 1000;
   2564 	vattr.va_mtime.tv_sec = tv[1].tv_sec;
   2565 	vattr.va_mtime.tv_nsec = tv[1].tv_usec * 1000;
   2566 	error = VOP_SETATTR(vp, &vattr, p->p_ucred, p);
   2567 	VOP_UNLOCK(vp, 0);
   2568 	return (error);
   2569 }
   2570 
   2571 /*
   2572  * Truncate a file given its path name.
   2573  */
   2574 /* ARGSUSED */
   2575 int
   2576 sys_truncate(p, v, retval)
   2577 	struct proc *p;
   2578 	void *v;
   2579 	register_t *retval;
   2580 {
   2581 	register struct sys_truncate_args /* {
   2582 		syscallarg(const char *) path;
   2583 		syscallarg(int) pad;
   2584 		syscallarg(off_t) length;
   2585 	} */ *uap = v;
   2586 	register struct vnode *vp;
   2587 	struct vattr vattr;
   2588 	int error;
   2589 	struct nameidata nd;
   2590 
   2591 	NDINIT(&nd, LOOKUP, FOLLOW, UIO_USERSPACE, SCARG(uap, path), p);
   2592 	if ((error = namei(&nd)) != 0)
   2593 		return (error);
   2594 	vp = nd.ni_vp;
   2595 	VOP_LEASE(vp, p, p->p_ucred, LEASE_WRITE);
   2596 	vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
   2597 	if (vp->v_type == VDIR)
   2598 		error = EISDIR;
   2599 	else if ((error = vn_writechk(vp)) == 0 &&
   2600 	    (error = VOP_ACCESS(vp, VWRITE, p->p_ucred, p)) == 0) {
   2601 		VATTR_NULL(&vattr);
   2602 		vattr.va_size = SCARG(uap, length);
   2603 		error = VOP_SETATTR(vp, &vattr, p->p_ucred, p);
   2604 	}
   2605 	vput(vp);
   2606 	return (error);
   2607 }
   2608 
   2609 /*
   2610  * Truncate a file given a file descriptor.
   2611  */
   2612 /* ARGSUSED */
   2613 int
   2614 sys_ftruncate(p, v, retval)
   2615 	struct proc *p;
   2616 	void *v;
   2617 	register_t *retval;
   2618 {
   2619 	register struct sys_ftruncate_args /* {
   2620 		syscallarg(int) fd;
   2621 		syscallarg(int) pad;
   2622 		syscallarg(off_t) length;
   2623 	} */ *uap = v;
   2624 	struct vattr vattr;
   2625 	struct vnode *vp;
   2626 	struct file *fp;
   2627 	int error;
   2628 
   2629 	/* getvnode() will use the descriptor for us */
   2630 	if ((error = getvnode(p->p_fd, SCARG(uap, fd), &fp)) != 0)
   2631 		return (error);
   2632 	if ((fp->f_flag & FWRITE) == 0) {
   2633 		error = EINVAL;
   2634 		goto out;
   2635 	}
   2636 	vp = (struct vnode *)fp->f_data;
   2637 	VOP_LEASE(vp, p, p->p_ucred, LEASE_WRITE);
   2638 	vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
   2639 	if (vp->v_type == VDIR)
   2640 		error = EISDIR;
   2641 	else if ((error = vn_writechk(vp)) == 0) {
   2642 		VATTR_NULL(&vattr);
   2643 		vattr.va_size = SCARG(uap, length);
   2644 		error = VOP_SETATTR(vp, &vattr, fp->f_cred, p);
   2645 	}
   2646 	VOP_UNLOCK(vp, 0);
   2647  out:
   2648 	FILE_UNUSE(fp, p);
   2649 	return (error);
   2650 }
   2651 
   2652 /*
   2653  * Sync an open file.
   2654  */
   2655 /* ARGSUSED */
   2656 int
   2657 sys_fsync(p, v, retval)
   2658 	struct proc *p;
   2659 	void *v;
   2660 	register_t *retval;
   2661 {
   2662 	struct sys_fsync_args /* {
   2663 		syscallarg(int) fd;
   2664 	} */ *uap = v;
   2665 	register struct vnode *vp;
   2666 	struct file *fp;
   2667 	int error;
   2668 
   2669 	/* getvnode() will use the descriptor for us */
   2670 	if ((error = getvnode(p->p_fd, SCARG(uap, fd), &fp)) != 0)
   2671 		return (error);
   2672 	vp = (struct vnode *)fp->f_data;
   2673 	vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
   2674 	error = VOP_FSYNC(vp, fp->f_cred, FSYNC_WAIT, p);
   2675 	if (error == 0 && bioops.io_fsync != NULL &&
   2676 	    vp->v_mount && (vp->v_mount->mnt_flag & MNT_SOFTDEP))
   2677 		(*bioops.io_fsync)(vp);
   2678 	VOP_UNLOCK(vp, 0);
   2679 	FILE_UNUSE(fp, p);
   2680 	return (error);
   2681 }
   2682 
   2683 /*
   2684  * Sync the data of an open file.
   2685  */
   2686 /* ARGSUSED */
   2687 int
   2688 sys_fdatasync(p, v, retval)
   2689 	struct proc *p;
   2690 	void *v;
   2691 	register_t *retval;
   2692 {
   2693 	struct sys_fdatasync_args /* {
   2694 		syscallarg(int) fd;
   2695 	} */ *uap = v;
   2696 	struct vnode *vp;
   2697 	struct file *fp;
   2698 	int error;
   2699 
   2700 	/* getvnode() will use the descriptor for us */
   2701 	if ((error = getvnode(p->p_fd, SCARG(uap, fd), &fp)) != 0)
   2702 		return (error);
   2703 	vp = (struct vnode *)fp->f_data;
   2704 	vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
   2705 	error = VOP_FSYNC(vp, fp->f_cred, FSYNC_WAIT|FSYNC_DATAONLY, p);
   2706 	if (error == 0 && bioops.io_fsync != NULL)
   2707 		(*bioops.io_fsync)(vp);
   2708 	VOP_UNLOCK(vp, 0);
   2709 	FILE_UNUSE(fp, p);
   2710 	return (error);
   2711 }
   2712 
   2713 /*
   2714  * Rename files, (standard) BSD semantics frontend.
   2715  */
   2716 /* ARGSUSED */
   2717 int
   2718 sys_rename(p, v, retval)
   2719 	struct proc *p;
   2720 	void *v;
   2721 	register_t *retval;
   2722 {
   2723 	register struct sys_rename_args /* {
   2724 		syscallarg(const char *) from;
   2725 		syscallarg(const char *) to;
   2726 	} */ *uap = v;
   2727 
   2728 	return (rename_files(SCARG(uap, from), SCARG(uap, to), p, 0));
   2729 }
   2730 
   2731 /*
   2732  * Rename files, POSIX semantics frontend.
   2733  */
   2734 /* ARGSUSED */
   2735 int
   2736 sys___posix_rename(p, v, retval)
   2737 	struct proc *p;
   2738 	void *v;
   2739 	register_t *retval;
   2740 {
   2741 	register struct sys___posix_rename_args /* {
   2742 		syscallarg(const char *) from;
   2743 		syscallarg(const char *) to;
   2744 	} */ *uap = v;
   2745 
   2746 	return (rename_files(SCARG(uap, from), SCARG(uap, to), p, 1));
   2747 }
   2748 
   2749 /*
   2750  * Rename files.  Source and destination must either both be directories,
   2751  * or both not be directories.  If target is a directory, it must be empty.
   2752  * If `from' and `to' refer to the same object, the value of the `retain'
   2753  * argument is used to determine whether `from' will be
   2754  *
   2755  * (retain == 0)	deleted unless `from' and `to' refer to the same
   2756  *			object in the file system's name space (BSD).
   2757  * (retain == 1)	always retained (POSIX).
   2758  */
   2759 static int
   2760 rename_files(from, to, p, retain)
   2761 	const char *from, *to;
   2762 	struct proc *p;
   2763 	int retain;
   2764 {
   2765 	register struct vnode *tvp, *fvp, *tdvp;
   2766 	struct nameidata fromnd, tond;
   2767 	int error;
   2768 
   2769 	NDINIT(&fromnd, DELETE, WANTPARENT | SAVESTART, UIO_USERSPACE,
   2770 	    from, p);
   2771 	if ((error = namei(&fromnd)) != 0)
   2772 		return (error);
   2773 	fvp = fromnd.ni_vp;
   2774 	NDINIT(&tond, RENAME, LOCKPARENT | LOCKLEAF | NOCACHE | SAVESTART,
   2775 	    UIO_USERSPACE, to, p);
   2776 	if ((error = namei(&tond)) != 0) {
   2777 		VOP_ABORTOP(fromnd.ni_dvp, &fromnd.ni_cnd);
   2778 		vrele(fromnd.ni_dvp);
   2779 		vrele(fvp);
   2780 		goto out1;
   2781 	}
   2782 	tdvp = tond.ni_dvp;
   2783 	tvp = tond.ni_vp;
   2784 
   2785 	if (tvp != NULL) {
   2786 		if (fvp->v_type == VDIR && tvp->v_type != VDIR) {
   2787 			error = ENOTDIR;
   2788 			goto out;
   2789 		} else if (fvp->v_type != VDIR && tvp->v_type == VDIR) {
   2790 			error = EISDIR;
   2791 			goto out;
   2792 		}
   2793 	}
   2794 
   2795 	if (fvp == tdvp)
   2796 		error = EINVAL;
   2797 
   2798 	/*
   2799 	 * Source and destination refer to the same object.
   2800 	 */
   2801 	if (fvp == tvp) {
   2802 		if (retain)
   2803 			error = -1;
   2804 		else if (fromnd.ni_dvp == tdvp &&
   2805 		    fromnd.ni_cnd.cn_namelen == tond.ni_cnd.cn_namelen &&
   2806 		    !memcmp(fromnd.ni_cnd.cn_nameptr,
   2807 		          tond.ni_cnd.cn_nameptr,
   2808 		          fromnd.ni_cnd.cn_namelen))
   2809 		error = -1;
   2810 	}
   2811 
   2812 out:
   2813 	if (!error) {
   2814 		VOP_LEASE(tdvp, p, p->p_ucred, LEASE_WRITE);
   2815 		if (fromnd.ni_dvp != tdvp)
   2816 			VOP_LEASE(fromnd.ni_dvp, p, p->p_ucred, LEASE_WRITE);
   2817 		if (tvp) {
   2818 			(void)uvm_vnp_uncache(tvp);
   2819 			VOP_LEASE(tvp, p, p->p_ucred, LEASE_WRITE);
   2820 		}
   2821 		error = VOP_RENAME(fromnd.ni_dvp, fromnd.ni_vp, &fromnd.ni_cnd,
   2822 				   tond.ni_dvp, tond.ni_vp, &tond.ni_cnd);
   2823 	} else {
   2824 		VOP_ABORTOP(tond.ni_dvp, &tond.ni_cnd);
   2825 		if (tdvp == tvp)
   2826 			vrele(tdvp);
   2827 		else
   2828 			vput(tdvp);
   2829 		if (tvp)
   2830 			vput(tvp);
   2831 		VOP_ABORTOP(fromnd.ni_dvp, &fromnd.ni_cnd);
   2832 		vrele(fromnd.ni_dvp);
   2833 		vrele(fvp);
   2834 	}
   2835 	vrele(tond.ni_startdir);
   2836 	FREE(tond.ni_cnd.cn_pnbuf, M_NAMEI);
   2837 out1:
   2838 	if (fromnd.ni_startdir)
   2839 		vrele(fromnd.ni_startdir);
   2840 	FREE(fromnd.ni_cnd.cn_pnbuf, M_NAMEI);
   2841 	return (error == -1 ? 0 : error);
   2842 }
   2843 
   2844 /*
   2845  * Make a directory file.
   2846  */
   2847 /* ARGSUSED */
   2848 int
   2849 sys_mkdir(p, v, retval)
   2850 	struct proc *p;
   2851 	void *v;
   2852 	register_t *retval;
   2853 {
   2854 	register struct sys_mkdir_args /* {
   2855 		syscallarg(const char *) path;
   2856 		syscallarg(int) mode;
   2857 	} */ *uap = v;
   2858 	register struct vnode *vp;
   2859 	struct vattr vattr;
   2860 	int error;
   2861 	struct nameidata nd;
   2862 
   2863 	NDINIT(&nd, CREATE, LOCKPARENT, UIO_USERSPACE, SCARG(uap, path), p);
   2864 	if ((error = namei(&nd)) != 0)
   2865 		return (error);
   2866 	vp = nd.ni_vp;
   2867 	if (vp != NULL) {
   2868 		VOP_ABORTOP(nd.ni_dvp, &nd.ni_cnd);
   2869 		if (nd.ni_dvp == vp)
   2870 			vrele(nd.ni_dvp);
   2871 		else
   2872 			vput(nd.ni_dvp);
   2873 		vrele(vp);
   2874 		return (EEXIST);
   2875 	}
   2876 	VATTR_NULL(&vattr);
   2877 	vattr.va_type = VDIR;
   2878 	vattr.va_mode =
   2879 	    (SCARG(uap, mode) & ACCESSPERMS) &~ p->p_cwdi->cwdi_cmask;
   2880 	VOP_LEASE(nd.ni_dvp, p, p->p_ucred, LEASE_WRITE);
   2881 	error = VOP_MKDIR(nd.ni_dvp, &nd.ni_vp, &nd.ni_cnd, &vattr);
   2882 	if (!error)
   2883 		vput(nd.ni_vp);
   2884 	return (error);
   2885 }
   2886 
   2887 /*
   2888  * Remove a directory file.
   2889  */
   2890 /* ARGSUSED */
   2891 int
   2892 sys_rmdir(p, v, retval)
   2893 	struct proc *p;
   2894 	void *v;
   2895 	register_t *retval;
   2896 {
   2897 	struct sys_rmdir_args /* {
   2898 		syscallarg(const char *) path;
   2899 	} */ *uap = v;
   2900 	register struct vnode *vp;
   2901 	int error;
   2902 	struct nameidata nd;
   2903 
   2904 	NDINIT(&nd, DELETE, LOCKPARENT | LOCKLEAF, UIO_USERSPACE,
   2905 	    SCARG(uap, path), p);
   2906 	if ((error = namei(&nd)) != 0)
   2907 		return (error);
   2908 	vp = nd.ni_vp;
   2909 	if (vp->v_type != VDIR) {
   2910 		error = ENOTDIR;
   2911 		goto out;
   2912 	}
   2913 	/*
   2914 	 * No rmdir "." please.
   2915 	 */
   2916 	if (nd.ni_dvp == vp) {
   2917 		error = EINVAL;
   2918 		goto out;
   2919 	}
   2920 	/*
   2921 	 * The root of a mounted filesystem cannot be deleted.
   2922 	 */
   2923 	if (vp->v_flag & VROOT)
   2924 		error = EBUSY;
   2925 out:
   2926 	if (!error) {
   2927 		VOP_LEASE(nd.ni_dvp, p, p->p_ucred, LEASE_WRITE);
   2928 		VOP_LEASE(vp, p, p->p_ucred, LEASE_WRITE);
   2929 		error = VOP_RMDIR(nd.ni_dvp, nd.ni_vp, &nd.ni_cnd);
   2930 	} else {
   2931 		VOP_ABORTOP(nd.ni_dvp, &nd.ni_cnd);
   2932 		if (nd.ni_dvp == vp)
   2933 			vrele(nd.ni_dvp);
   2934 		else
   2935 			vput(nd.ni_dvp);
   2936 		vput(vp);
   2937 	}
   2938 	return (error);
   2939 }
   2940 
   2941 /*
   2942  * Read a block of directory entries in a file system independent format.
   2943  */
   2944 int
   2945 sys_getdents(p, v, retval)
   2946 	struct proc *p;
   2947 	void *v;
   2948 	register_t *retval;
   2949 {
   2950 	register struct sys_getdents_args /* {
   2951 		syscallarg(int) fd;
   2952 		syscallarg(char *) buf;
   2953 		syscallarg(size_t) count;
   2954 	} */ *uap = v;
   2955 	struct file *fp;
   2956 	int error, done;
   2957 
   2958 	/* getvnode() will use the descriptor for us */
   2959 	if ((error = getvnode(p->p_fd, SCARG(uap, fd), &fp)) != 0)
   2960 		return (error);
   2961 	if ((fp->f_flag & FREAD) == 0) {
   2962 		error = EBADF;
   2963 		goto out;
   2964 	}
   2965 	error = vn_readdir(fp, SCARG(uap, buf), UIO_USERSPACE,
   2966 			SCARG(uap, count), &done, p, 0, 0);
   2967 	*retval = done;
   2968  out:
   2969 	FILE_UNUSE(fp, p);
   2970 	return (error);
   2971 }
   2972 
   2973 /*
   2974  * Set the mode mask for creation of filesystem nodes.
   2975  */
   2976 int
   2977 sys_umask(p, v, retval)
   2978 	struct proc *p;
   2979 	void *v;
   2980 	register_t *retval;
   2981 {
   2982 	struct sys_umask_args /* {
   2983 		syscallarg(mode_t) newmask;
   2984 	} */ *uap = v;
   2985 	struct cwdinfo *cwdi;
   2986 
   2987 	cwdi = p->p_cwdi;
   2988 	*retval = cwdi->cwdi_cmask;
   2989 	cwdi->cwdi_cmask = SCARG(uap, newmask) & ALLPERMS;
   2990 	return (0);
   2991 }
   2992 
   2993 /*
   2994  * Void all references to file by ripping underlying filesystem
   2995  * away from vnode.
   2996  */
   2997 /* ARGSUSED */
   2998 int
   2999 sys_revoke(p, v, retval)
   3000 	struct proc *p;
   3001 	void *v;
   3002 	register_t *retval;
   3003 {
   3004 	register struct sys_revoke_args /* {
   3005 		syscallarg(const char *) path;
   3006 	} */ *uap = v;
   3007 	register struct vnode *vp;
   3008 	struct vattr vattr;
   3009 	int error;
   3010 	struct nameidata nd;
   3011 
   3012 	NDINIT(&nd, LOOKUP, FOLLOW, UIO_USERSPACE, SCARG(uap, path), p);
   3013 	if ((error = namei(&nd)) != 0)
   3014 		return (error);
   3015 	vp = nd.ni_vp;
   3016 	if ((error = VOP_GETATTR(vp, &vattr, p->p_ucred, p)) != 0)
   3017 		goto out;
   3018 	if (p->p_ucred->cr_uid != vattr.va_uid &&
   3019 	    (error = suser(p->p_ucred, &p->p_acflag)) != 0)
   3020 		goto out;
   3021 	if (vp->v_usecount > 1 || (vp->v_flag & (VALIASED | VLAYER)))
   3022 		VOP_REVOKE(vp, REVOKEALL);
   3023 out:
   3024 	vrele(vp);
   3025 	return (error);
   3026 }
   3027 
   3028 /*
   3029  * Convert a user file descriptor to a kernel file entry.
   3030  */
   3031 int
   3032 getvnode(fdp, fd, fpp)
   3033 	struct filedesc *fdp;
   3034 	int fd;
   3035 	struct file **fpp;
   3036 {
   3037 	struct vnode *vp;
   3038 	struct file *fp;
   3039 
   3040 	if ((u_int)fd >= fdp->fd_nfiles ||
   3041 	    (fp = fdp->fd_ofiles[fd]) == NULL ||
   3042 	    (fp->f_iflags & FIF_WANTCLOSE) != 0)
   3043 		return (EBADF);
   3044 
   3045 	FILE_USE(fp);
   3046 
   3047 	if (fp->f_type != DTYPE_VNODE) {
   3048 		FILE_UNUSE(fp, NULL);
   3049 		return (EINVAL);
   3050 	}
   3051 
   3052 	vp = (struct vnode *)fp->f_data;
   3053 	if (vp->v_type == VBAD) {
   3054 		FILE_UNUSE(fp, NULL);
   3055 		return (EBADF);
   3056 	}
   3057 
   3058 	*fpp = fp;
   3059 	return (0);
   3060 }
   3061