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