xref: /dragonfly/sys/kern/vfs_syscalls.c (revision 8dbfb057)
1 /*
2  * Copyright (c) 1989, 1993
3  *	The Regents of the University of California.  All rights reserved.
4  * (c) UNIX System Laboratories, Inc.
5  * All or some portions of this file are derived from material licensed
6  * to the University of California by American Telephone and Telegraph
7  * Co. or Unix System Laboratories, Inc. and are reproduced herein with
8  * the permission of UNIX System Laboratories, Inc.
9  *
10  * Redistribution and use in source and binary forms, with or without
11  * modification, are permitted provided that the following conditions
12  * are met:
13  * 1. Redistributions of source code must retain the above copyright
14  *    notice, this list of conditions and the following disclaimer.
15  * 2. Redistributions in binary form must reproduce the above copyright
16  *    notice, this list of conditions and the following disclaimer in the
17  *    documentation and/or other materials provided with the distribution.
18  * 3. All advertising materials mentioning features or use of this software
19  *    must display the following acknowledgement:
20  *	This product includes software developed by the University of
21  *	California, Berkeley and its contributors.
22  * 4. Neither the name of the University nor the names of its contributors
23  *    may be used to endorse or promote products derived from this software
24  *    without specific prior written permission.
25  *
26  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
27  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
28  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
29  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
30  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
31  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
32  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
33  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
34  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
35  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
36  * SUCH DAMAGE.
37  *
38  *	@(#)vfs_syscalls.c	8.13 (Berkeley) 4/15/94
39  * $FreeBSD: src/sys/kern/vfs_syscalls.c,v 1.151.2.18 2003/04/04 20:35:58 tegge Exp $
40  * $DragonFly: src/sys/kern/vfs_syscalls.c,v 1.48 2004/11/24 08:37:16 dillon Exp $
41  */
42 
43 #include <sys/param.h>
44 #include <sys/systm.h>
45 #include <sys/buf.h>
46 #include <sys/conf.h>
47 #include <sys/sysent.h>
48 #include <sys/malloc.h>
49 #include <sys/mount.h>
50 #include <sys/sysproto.h>
51 #include <sys/filedesc.h>
52 #include <sys/kernel.h>
53 #include <sys/fcntl.h>
54 #include <sys/file.h>
55 #include <sys/linker.h>
56 #include <sys/stat.h>
57 #include <sys/unistd.h>
58 #include <sys/vnode.h>
59 #include <sys/proc.h>
60 #include <sys/namei.h>
61 #include <sys/nlookup.h>
62 #include <sys/dirent.h>
63 #include <sys/extattr.h>
64 #include <sys/kern_syscall.h>
65 
66 #include <machine/limits.h>
67 #include <vfs/union/union.h>
68 #include <sys/sysctl.h>
69 #include <vm/vm.h>
70 #include <vm/vm_object.h>
71 #include <vm/vm_zone.h>
72 #include <vm/vm_page.h>
73 
74 #include <sys/file2.h>
75 
76 static int checkvp_chdir (struct vnode *vn, struct thread *td);
77 static void checkdirs (struct vnode *olddp, struct namecache *ncp);
78 static int chroot_refuse_vdir_fds (struct filedesc *fdp);
79 static int getutimes (const struct timeval *, struct timespec *);
80 static int setfown (struct vnode *, uid_t, gid_t);
81 static int setfmode (struct vnode *, int);
82 static int setfflags (struct vnode *, int);
83 static int setutimes (struct vnode *, const struct timespec *, int);
84 static int	usermount = 0;	/* if 1, non-root can mount fs. */
85 
86 int (*union_dircheckp) (struct thread *, struct vnode **, struct file *);
87 
88 SYSCTL_INT(_vfs, OID_AUTO, usermount, CTLFLAG_RW, &usermount, 0, "");
89 
90 /*
91  * Virtual File System System Calls
92  */
93 
94 /*
95  * Mount a file system.
96  */
97 /*
98  * mount_args(char *type, char *path, int flags, caddr_t data)
99  */
100 /* ARGSUSED */
101 int
102 mount(struct mount_args *uap)
103 {
104 	struct thread *td = curthread;
105 	struct proc *p = td->td_proc;
106 	struct vnode *vp;
107 	struct namecache *ncp;
108 	struct mount *mp;
109 	struct vfsconf *vfsp;
110 	int error, flag = 0, flag2 = 0;
111 	struct vattr va;
112 	struct nlookupdata nd;
113 	char fstypename[MFSNAMELEN];
114 	lwkt_tokref ilock;
115 	struct nlcomponent nlc;
116 
117 	KKASSERT(p);
118 	if (p->p_ucred->cr_prison != NULL)
119 		return (EPERM);
120 	if (usermount == 0 && (error = suser(td)))
121 		return (error);
122 	/*
123 	 * Do not allow NFS export by non-root users.
124 	 */
125 	if (SCARG(uap, flags) & MNT_EXPORTED) {
126 		error = suser(td);
127 		if (error)
128 			return (error);
129 	}
130 	/*
131 	 * Silently enforce MNT_NOSUID and MNT_NODEV for non-root users
132 	 */
133 	if (suser(td))
134 		SCARG(uap, flags) |= MNT_NOSUID | MNT_NODEV;
135 
136 	/*
137 	 * Lookup the requested path and extract the ncp and vnode.
138 	 */
139 	error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
140 	if (error == 0) {
141 		if ((error = nlookup(&nd)) == 0) {
142 			if (nd.nl_ncp->nc_vp == NULL)
143 				error = ENOENT;
144 		}
145 	}
146 	if (error) {
147 		nlookup_done(&nd);
148 		return (error);
149 	}
150 
151 	/*
152 	 * Extract the locked+refd ncp and cleanup the nd structure
153 	 */
154 	ncp = nd.nl_ncp;
155 	nd.nl_ncp = NULL;
156 	nlookup_done(&nd);
157 
158 	/*
159 	 * now we have the locked ref'd ncp and unreferenced vnode.
160 	 */
161 	vp = ncp->nc_vp;
162 	if ((error = vget(vp, LK_EXCLUSIVE, td)) != 0) {
163 		cache_put(ncp);
164 		return (error);
165 	}
166 	cache_unlock(ncp);
167 
168 	/*
169 	 * Now we have an unlocked ref'd ncp and a locked ref'd vp
170 	 */
171 	if (SCARG(uap, flags) & MNT_UPDATE) {
172 		if ((vp->v_flag & VROOT) == 0) {
173 			cache_drop(ncp);
174 			vput(vp);
175 			return (EINVAL);
176 		}
177 		mp = vp->v_mount;
178 		flag = mp->mnt_flag;
179 		flag2 = mp->mnt_kern_flag;
180 		/*
181 		 * We only allow the filesystem to be reloaded if it
182 		 * is currently mounted read-only.
183 		 */
184 		if ((SCARG(uap, flags) & MNT_RELOAD) &&
185 		    ((mp->mnt_flag & MNT_RDONLY) == 0)) {
186 			cache_drop(ncp);
187 			vput(vp);
188 			return (EOPNOTSUPP);	/* Needs translation */
189 		}
190 		/*
191 		 * Only root, or the user that did the original mount is
192 		 * permitted to update it.
193 		 */
194 		if (mp->mnt_stat.f_owner != p->p_ucred->cr_uid &&
195 		    (error = suser(td))) {
196 			cache_drop(ncp);
197 			vput(vp);
198 			return (error);
199 		}
200 		if (vfs_busy(mp, LK_NOWAIT, NULL, td)) {
201 			cache_drop(ncp);
202 			vput(vp);
203 			return (EBUSY);
204 		}
205 		if ((vp->v_flag & VMOUNT) != 0 ||
206 		    vp->v_mountedhere != NULL) {
207 			cache_drop(ncp);
208 			vfs_unbusy(mp, td);
209 			vput(vp);
210 			return (EBUSY);
211 		}
212 		vp->v_flag |= VMOUNT;
213 		mp->mnt_flag |=
214 		    SCARG(uap, flags) & (MNT_RELOAD | MNT_FORCE | MNT_UPDATE);
215 		VOP_UNLOCK(vp, 0, td);
216 		goto update;
217 	}
218 	/*
219 	 * If the user is not root, ensure that they own the directory
220 	 * onto which we are attempting to mount.
221 	 */
222 	if ((error = VOP_GETATTR(vp, &va, td)) ||
223 	    (va.va_uid != p->p_ucred->cr_uid &&
224 	     (error = suser(td)))) {
225 		cache_drop(ncp);
226 		vput(vp);
227 		return (error);
228 	}
229 	if ((error = vinvalbuf(vp, V_SAVE, td, 0, 0)) != 0) {
230 		cache_drop(ncp);
231 		vput(vp);
232 		return (error);
233 	}
234 	if (vp->v_type != VDIR) {
235 		cache_drop(ncp);
236 		vput(vp);
237 		return (ENOTDIR);
238 	}
239 	if ((error = copyinstr(SCARG(uap, type), fstypename, MFSNAMELEN, NULL)) != 0) {
240 		cache_drop(ncp);
241 		vput(vp);
242 		return (error);
243 	}
244 	for (vfsp = vfsconf; vfsp; vfsp = vfsp->vfc_next) {
245 		if (!strcmp(vfsp->vfc_name, fstypename))
246 			break;
247 	}
248 	if (vfsp == NULL) {
249 		linker_file_t lf;
250 
251 		/* Only load modules for root (very important!) */
252 		if ((error = suser(td)) != 0) {
253 			cache_drop(ncp);
254 			vput(vp);
255 			return error;
256 		}
257 		error = linker_load_file(fstypename, &lf);
258 		if (error || lf == NULL) {
259 			cache_drop(ncp);
260 			vput(vp);
261 			if (lf == NULL)
262 				error = ENODEV;
263 			return error;
264 		}
265 		lf->userrefs++;
266 		/* lookup again, see if the VFS was loaded */
267 		for (vfsp = vfsconf; vfsp; vfsp = vfsp->vfc_next) {
268 			if (!strcmp(vfsp->vfc_name, fstypename))
269 				break;
270 		}
271 		if (vfsp == NULL) {
272 			lf->userrefs--;
273 			linker_file_unload(lf);
274 			cache_drop(ncp);
275 			vput(vp);
276 			return (ENODEV);
277 		}
278 	}
279 	if ((vp->v_flag & VMOUNT) != 0 ||
280 	    vp->v_mountedhere != NULL) {
281 		cache_drop(ncp);
282 		vput(vp);
283 		return (EBUSY);
284 	}
285 	vp->v_flag |= VMOUNT;
286 
287 	/*
288 	 * Allocate and initialize the filesystem.
289 	 */
290 	mp = malloc(sizeof(struct mount), M_MOUNT, M_ZERO|M_WAITOK);
291 	TAILQ_INIT(&mp->mnt_nvnodelist);
292 	TAILQ_INIT(&mp->mnt_reservedvnlist);
293 	mp->mnt_nvnodelistsize = 0;
294 	lockinit(&mp->mnt_lock, 0, "vfslock", 0, LK_NOPAUSE);
295 	vfs_busy(mp, LK_NOWAIT, NULL, td);
296 	mp->mnt_op = vfsp->vfc_vfsops;
297 	mp->mnt_vfc = vfsp;
298 	vfsp->vfc_refcount++;
299 	mp->mnt_stat.f_type = vfsp->vfc_typenum;
300 	mp->mnt_flag |= vfsp->vfc_flags & MNT_VISFLAGMASK;
301 	strncpy(mp->mnt_stat.f_fstypename, vfsp->vfc_name, MFSNAMELEN);
302 	mp->mnt_vnodecovered = vp;
303 	mp->mnt_stat.f_owner = p->p_ucred->cr_uid;
304 	mp->mnt_iosize_max = DFLTPHYS;
305 	VOP_UNLOCK(vp, 0, td);
306 update:
307 	/*
308 	 * Set the mount level flags.
309 	 */
310 	if (SCARG(uap, flags) & MNT_RDONLY)
311 		mp->mnt_flag |= MNT_RDONLY;
312 	else if (mp->mnt_flag & MNT_RDONLY)
313 		mp->mnt_kern_flag |= MNTK_WANTRDWR;
314 	mp->mnt_flag &=~ (MNT_NOSUID | MNT_NOEXEC | MNT_NODEV |
315 	    MNT_SYNCHRONOUS | MNT_UNION | MNT_ASYNC | MNT_NOATIME |
316 	    MNT_NOSYMFOLLOW | MNT_IGNORE |
317 	    MNT_NOCLUSTERR | MNT_NOCLUSTERW | MNT_SUIDDIR);
318 	mp->mnt_flag |= SCARG(uap, flags) & (MNT_NOSUID | MNT_NOEXEC |
319 	    MNT_NODEV | MNT_SYNCHRONOUS | MNT_UNION | MNT_ASYNC | MNT_FORCE |
320 	    MNT_NOSYMFOLLOW | MNT_IGNORE |
321 	    MNT_NOATIME | MNT_NOCLUSTERR | MNT_NOCLUSTERW | MNT_SUIDDIR);
322 	/*
323 	 * Mount the filesystem.
324 	 * XXX The final recipients of VFS_MOUNT just overwrite the ndp they
325 	 * get.
326 	 */
327 	error = VFS_MOUNT(mp, SCARG(uap, path), SCARG(uap, data), td);
328 	if (mp->mnt_flag & MNT_UPDATE) {
329 		if (mp->mnt_kern_flag & MNTK_WANTRDWR)
330 			mp->mnt_flag &= ~MNT_RDONLY;
331 		mp->mnt_flag &=~ (MNT_UPDATE | MNT_RELOAD | MNT_FORCE);
332 		mp->mnt_kern_flag &=~ MNTK_WANTRDWR;
333 		if (error) {
334 			mp->mnt_flag = flag;
335 			mp->mnt_kern_flag = flag2;
336 		}
337 		vfs_unbusy(mp, td);
338 		vp->v_flag &= ~VMOUNT;
339 		vrele(vp);
340 		cache_drop(ncp);
341 		return (error);
342 	}
343 	vn_lock(vp, LK_EXCLUSIVE | LK_RETRY, td);
344 	/*
345 	 * Put the new filesystem on the mount list after root.  The mount
346 	 * point gets its own mnt_ncp which is a special ncp linking the
347 	 * vnode-under to the root of the new mount.  The lookup code
348 	 * detects the mount point going forward and detects the special
349 	 * mnt_ncp via NCP_MOUNTPT going backwards.
350 	 *
351 	 * It is not necessary to invalidate or purge the vnode underneath
352 	 * because elements under the mount will be given their own glue
353 	 * namecache record.
354 	 */
355 	if (!error) {
356 		nlc.nlc_nameptr = "";
357 		nlc.nlc_namelen = 0;
358 		mp->mnt_ncp = cache_nlookup(ncp, &nlc);
359 		mp->mnt_ncp->nc_flag |= NCF_MOUNTPT;
360 		mp->mnt_ncp->nc_mount = mp;
361 		cache_drop(ncp);
362 		/* XXX get the root of the fs and cache_setvp(mnt_ncp...) */
363 		vp->v_flag &= ~VMOUNT;
364 		vp->v_mountedhere = mp;
365 		lwkt_gettoken(&ilock, &mountlist_token);
366 		TAILQ_INSERT_TAIL(&mountlist, mp, mnt_list);
367 		lwkt_reltoken(&ilock);
368 		checkdirs(vp, mp->mnt_ncp);
369 		cache_unlock(mp->mnt_ncp);	/* leave ref intact */
370 		VOP_UNLOCK(vp, 0, td);
371 		error = vfs_allocate_syncvnode(mp);
372 		vfs_unbusy(mp, td);
373 		if ((error = VFS_START(mp, 0, td)) != 0)
374 			vrele(vp);
375 	} else {
376 		vfs_rm_vnodeops(&mp->mnt_vn_ops);
377 		vfs_rm_vnodeops(&mp->mnt_vn_spec_ops);
378 		vfs_rm_vnodeops(&mp->mnt_vn_fifo_ops);
379 		vp->v_flag &= ~VMOUNT;
380 		mp->mnt_vfc->vfc_refcount--;
381 		vfs_unbusy(mp, td);
382 		free(mp, M_MOUNT);
383 		cache_drop(ncp);
384 		vput(vp);
385 	}
386 	return (error);
387 }
388 
389 /*
390  * Scan all active processes to see if any of them have a current
391  * or root directory onto which the new filesystem has just been
392  * mounted. If so, replace them with the new mount point.
393  *
394  * The passed ncp is ref'd and locked (from the mount code) and
395  * must be associated with the vnode representing the root of the
396  * mount point.
397  */
398 static void
399 checkdirs(struct vnode *olddp, struct namecache *ncp)
400 {
401 	struct filedesc *fdp;
402 	struct vnode *newdp;
403 	struct mount *mp;
404 	struct proc *p;
405 
406 	if (olddp->v_usecount == 1)
407 		return;
408 	mp = olddp->v_mountedhere;
409 	if (VFS_ROOT(mp, &newdp))
410 		panic("mount: lost mount");
411 	cache_setvp(ncp, newdp);
412 
413 	if (rootvnode == olddp) {
414 		vref(newdp);
415 		vfs_cache_setroot(newdp, cache_hold(ncp));
416 	}
417 
418 	FOREACH_PROC_IN_SYSTEM(p) {
419 		fdp = p->p_fd;
420 		if (fdp->fd_cdir == olddp) {
421 			vrele(fdp->fd_cdir);
422 			vref(newdp);
423 			fdp->fd_cdir = newdp;
424 			cache_drop(fdp->fd_ncdir);
425 			fdp->fd_ncdir = cache_hold(ncp);
426 		}
427 		if (fdp->fd_rdir == olddp) {
428 			vrele(fdp->fd_rdir);
429 			vref(newdp);
430 			fdp->fd_rdir = newdp;
431 			cache_drop(fdp->fd_nrdir);
432 			fdp->fd_nrdir = cache_hold(ncp);
433 		}
434 	}
435 	vput(newdp);
436 }
437 
438 /*
439  * Unmount a file system.
440  *
441  * Note: unmount takes a path to the vnode mounted on as argument,
442  * not special file (as before).
443  */
444 /*
445  * umount_args(char *path, int flags)
446  */
447 /* ARGSUSED */
448 int
449 unmount(struct unmount_args *uap)
450 {
451 	struct thread *td = curthread;
452 	struct proc *p = td->td_proc;
453 	struct vnode *vp;
454 	struct mount *mp;
455 	int error;
456 	struct nlookupdata nd;
457 
458 	KKASSERT(p);
459 	if (p->p_ucred->cr_prison != NULL)
460 		return (EPERM);
461 	if (usermount == 0 && (error = suser(td)))
462 		return (error);
463 
464 	vp = NULL;
465 	error = nlookup_init(&nd, SCARG(uap, path), UIO_USERSPACE, NLC_FOLLOW);
466 	if (error == 0)
467 		error = nlookup(&nd);
468 	if (error == 0)
469 		error = cache_vget(nd.nl_ncp, nd.nl_cred, LK_EXCLUSIVE, &vp);
470 	nlookup_done(&nd);
471 	if (error)
472 		return (error);
473 
474 	mp = vp->v_mount;
475 
476 	/*
477 	 * Only root, or the user that did the original mount is
478 	 * permitted to unmount this filesystem.
479 	 */
480 	if ((mp->mnt_stat.f_owner != p->p_ucred->cr_uid) &&
481 	    (error = suser(td))) {
482 		vput(vp);
483 		return (error);
484 	}
485 
486 	/*
487 	 * Don't allow unmounting the root file system.
488 	 */
489 	if (mp->mnt_flag & MNT_ROOTFS) {
490 		vput(vp);
491 		return (EINVAL);
492 	}
493 
494 	/*
495 	 * Must be the root of the filesystem
496 	 */
497 	if ((vp->v_flag & VROOT) == 0) {
498 		vput(vp);
499 		return (EINVAL);
500 	}
501 	vput(vp);
502 	return (dounmount(mp, SCARG(uap, flags), td));
503 }
504 
505 /*
506  * Do the actual file system unmount.
507  */
508 int
509 dounmount(struct mount *mp, int flags, struct thread *td)
510 {
511 	struct vnode *coveredvp;
512 	int error;
513 	int async_flag;
514 	lwkt_tokref ilock;
515 
516 	lwkt_gettoken(&ilock, &mountlist_token);
517 	if (mp->mnt_kern_flag & MNTK_UNMOUNT) {
518 		lwkt_reltoken(&ilock);
519 		return (EBUSY);
520 	}
521 	mp->mnt_kern_flag |= MNTK_UNMOUNT;
522 	/* Allow filesystems to detect that a forced unmount is in progress. */
523 	if (flags & MNT_FORCE)
524 		mp->mnt_kern_flag |= MNTK_UNMOUNTF;
525 	error = lockmgr(&mp->mnt_lock, LK_DRAIN | LK_INTERLOCK |
526 	    ((flags & MNT_FORCE) ? 0 : LK_NOWAIT), &ilock, td);
527 	if (error) {
528 		mp->mnt_kern_flag &= ~(MNTK_UNMOUNT | MNTK_UNMOUNTF);
529 		if (mp->mnt_kern_flag & MNTK_MWAIT)
530 			wakeup(mp);
531 		return (error);
532 	}
533 
534 	if (mp->mnt_flag & MNT_EXPUBLIC)
535 		vfs_setpublicfs(NULL, NULL, NULL);
536 
537 	vfs_msync(mp, MNT_WAIT);
538 	async_flag = mp->mnt_flag & MNT_ASYNC;
539 	mp->mnt_flag &=~ MNT_ASYNC;
540 	cache_purgevfs(mp);	/* remove cache entries for this file sys */
541 	if (mp->mnt_syncer != NULL)
542 		vrele(mp->mnt_syncer);
543 	if (((mp->mnt_flag & MNT_RDONLY) ||
544 	     (error = VFS_SYNC(mp, MNT_WAIT, td)) == 0) ||
545 	    (flags & MNT_FORCE))
546 		error = VFS_UNMOUNT(mp, flags, td);
547 	lwkt_gettokref(&ilock);
548 	if (error) {
549 		if (mp->mnt_syncer == NULL)
550 			vfs_allocate_syncvnode(mp);
551 		mp->mnt_kern_flag &= ~(MNTK_UNMOUNT | MNTK_UNMOUNTF);
552 		mp->mnt_flag |= async_flag;
553 		lockmgr(&mp->mnt_lock, LK_RELEASE | LK_INTERLOCK | LK_REENABLE,
554 		    &ilock, td);
555 		if (mp->mnt_kern_flag & MNTK_MWAIT)
556 			wakeup(mp);
557 		return (error);
558 	}
559 	TAILQ_REMOVE(&mountlist, mp, mnt_list);
560 
561 	/*
562 	 * Remove any installed vnode ops here so the individual VFSs don't
563 	 * have to.
564 	 */
565 	vfs_rm_vnodeops(&mp->mnt_vn_ops);
566 	vfs_rm_vnodeops(&mp->mnt_vn_spec_ops);
567 	vfs_rm_vnodeops(&mp->mnt_vn_fifo_ops);
568 
569 	if ((coveredvp = mp->mnt_vnodecovered) != NULLVP) {
570 		coveredvp->v_mountedhere = NULL;
571 		vrele(coveredvp);
572 		cache_drop(mp->mnt_ncp);
573 		mp->mnt_ncp = NULL;
574 	}
575 	mp->mnt_vfc->vfc_refcount--;
576 	if (!TAILQ_EMPTY(&mp->mnt_nvnodelist))
577 		panic("unmount: dangling vnode");
578 	lockmgr(&mp->mnt_lock, LK_RELEASE | LK_INTERLOCK, &ilock, td);
579 	if (mp->mnt_kern_flag & MNTK_MWAIT)
580 		wakeup(mp);
581 	free(mp, M_MOUNT);
582 	return (0);
583 }
584 
585 /*
586  * Sync each mounted filesystem.
587  */
588 
589 #ifdef DEBUG
590 static int syncprt = 0;
591 SYSCTL_INT(_debug, OID_AUTO, syncprt, CTLFLAG_RW, &syncprt, 0, "");
592 #endif /* DEBUG */
593 
594 /* ARGSUSED */
595 int
596 sync(struct sync_args *uap)
597 {
598 	struct thread *td = curthread;
599 	struct mount *mp, *nmp;
600 	lwkt_tokref ilock;
601 	int asyncflag;
602 
603 	lwkt_gettoken(&ilock, &mountlist_token);
604 	for (mp = TAILQ_FIRST(&mountlist); mp != NULL; mp = nmp) {
605 		if (vfs_busy(mp, LK_NOWAIT, &ilock, td)) {
606 			nmp = TAILQ_NEXT(mp, mnt_list);
607 			continue;
608 		}
609 		if ((mp->mnt_flag & MNT_RDONLY) == 0) {
610 			asyncflag = mp->mnt_flag & MNT_ASYNC;
611 			mp->mnt_flag &= ~MNT_ASYNC;
612 			vfs_msync(mp, MNT_NOWAIT);
613 			VFS_SYNC(mp, MNT_NOWAIT, td);
614 			mp->mnt_flag |= asyncflag;
615 		}
616 		lwkt_gettokref(&ilock);
617 		nmp = TAILQ_NEXT(mp, mnt_list);
618 		vfs_unbusy(mp, td);
619 	}
620 	lwkt_reltoken(&ilock);
621 /*
622  * print out buffer pool stat information on each sync() call.
623  */
624 #ifdef DEBUG
625 	if (syncprt)
626 		vfs_bufstats();
627 #endif /* DEBUG */
628 	return (0);
629 }
630 
631 /* XXX PRISON: could be per prison flag */
632 static int prison_quotas;
633 #if 0
634 SYSCTL_INT(_kern_prison, OID_AUTO, quotas, CTLFLAG_RW, &prison_quotas, 0, "");
635 #endif
636 
637 /*
638  *  quotactl_args(char *path, int fcmd, int uid, caddr_t arg)
639  *
640  * Change filesystem quotas.
641  */
642 /* ARGSUSED */
643 int
644 quotactl(struct quotactl_args *uap)
645 {
646 	struct nlookupdata nd;
647 	struct thread *td;
648 	struct proc *p;
649 	struct mount *mp;
650 	int error;
651 
652 	td = curthread;
653 	p = td->td_proc;
654 	if (p->p_ucred->cr_prison && !prison_quotas)
655 		return (EPERM);
656 
657 	error = nlookup_init(&nd, SCARG(uap, path), UIO_USERSPACE, NLC_FOLLOW);
658 	if (error == 0)
659 		error = nlookup(&nd);
660 	if (error == 0) {
661 		mp = nd.nl_ncp->nc_mount;
662 		error = VFS_QUOTACTL(mp, SCARG(uap, cmd), SCARG(uap, uid),
663 				    SCARG(uap, arg), nd.nl_td);
664 	}
665 	nlookup_done(&nd);
666 	return (error);
667 }
668 
669 int
670 kern_statfs(struct nlookupdata *nd, struct statfs *buf)
671 {
672 	struct thread *td = curthread;
673 	struct mount *mp;
674 	struct statfs *sp;
675 	int error;
676 
677 	if ((error = nlookup(nd)) != 0)
678 		return (error);
679 	mp = nd->nl_ncp->nc_mount;
680 	sp = &mp->mnt_stat;
681 	if ((error = VFS_STATFS(mp, sp, td)) != 0)
682 		return (error);
683 	sp->f_flags = mp->mnt_flag & MNT_VISFLAGMASK;
684 	bcopy(sp, buf, sizeof(*buf));
685 	/* Only root should have access to the fsid's. */
686 	if (suser(td))
687 		buf->f_fsid.val[0] = buf->f_fsid.val[1] = 0;
688 	return (0);
689 }
690 
691 /*
692  * statfs_args(char *path, struct statfs *buf)
693  *
694  * Get filesystem statistics.
695  */
696 int
697 statfs(struct statfs_args *uap)
698 {
699 	struct nlookupdata nd;
700 	struct statfs buf;
701 	int error;
702 
703 	error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
704 	if (error == 0)
705 		error = kern_statfs(&nd, &buf);
706 	nlookup_done(&nd);
707 	if (error == 0)
708 		error = copyout(&buf, uap->buf, sizeof(*uap->buf));
709 	return (error);
710 }
711 
712 int
713 kern_fstatfs(int fd, struct statfs *buf)
714 {
715 	struct thread *td = curthread;
716 	struct proc *p = td->td_proc;
717 	struct file *fp;
718 	struct mount *mp;
719 	struct statfs *sp;
720 	int error;
721 
722 	KKASSERT(p);
723 	error = getvnode(p->p_fd, fd, &fp);
724 	if (error)
725 		return (error);
726 	mp = ((struct vnode *)fp->f_data)->v_mount;
727 	if (mp == NULL)
728 		return (EBADF);
729 	sp = &mp->mnt_stat;
730 	error = VFS_STATFS(mp, sp, td);
731 	if (error)
732 		return (error);
733 	sp->f_flags = mp->mnt_flag & MNT_VISFLAGMASK;
734 	bcopy(sp, buf, sizeof(*buf));
735 	/* Only root should have access to the fsid's. */
736 	if (suser(td))
737 		buf->f_fsid.val[0] = buf->f_fsid.val[1] = 0;
738 	return (0);
739 }
740 
741 /*
742  * fstatfs_args(int fd, struct statfs *buf)
743  *
744  * Get filesystem statistics.
745  */
746 int
747 fstatfs(struct fstatfs_args *uap)
748 {
749 	struct statfs buf;
750 	int error;
751 
752 	error = kern_fstatfs(uap->fd, &buf);
753 
754 	if (error == 0)
755 		error = copyout(&buf, uap->buf, sizeof(*uap->buf));
756 	return (error);
757 }
758 
759 /*
760  * getfsstat_args(struct statfs *buf, long bufsize, int flags)
761  *
762  * Get statistics on all filesystems.
763  */
764 /* ARGSUSED */
765 int
766 getfsstat(struct getfsstat_args *uap)
767 {
768 	struct thread *td = curthread;
769 	struct mount *mp, *nmp;
770 	struct statfs *sp;
771 	caddr_t sfsp;
772 	lwkt_tokref ilock;
773 	long count, maxcount, error;
774 
775 	maxcount = SCARG(uap, bufsize) / sizeof(struct statfs);
776 	sfsp = (caddr_t)SCARG(uap, buf);
777 	count = 0;
778 	lwkt_gettoken(&ilock, &mountlist_token);
779 	for (mp = TAILQ_FIRST(&mountlist); mp != NULL; mp = nmp) {
780 		if (vfs_busy(mp, LK_NOWAIT, &ilock, td)) {
781 			nmp = TAILQ_NEXT(mp, mnt_list);
782 			continue;
783 		}
784 		if (sfsp && count < maxcount) {
785 			sp = &mp->mnt_stat;
786 			/*
787 			 * If MNT_NOWAIT or MNT_LAZY is specified, do not
788 			 * refresh the fsstat cache. MNT_NOWAIT or MNT_LAZY
789 			 * overrides MNT_WAIT.
790 			 */
791 			if (((SCARG(uap, flags) & (MNT_LAZY|MNT_NOWAIT)) == 0 ||
792 			    (SCARG(uap, flags) & MNT_WAIT)) &&
793 			    (error = VFS_STATFS(mp, sp, td))) {
794 				lwkt_gettokref(&ilock);
795 				nmp = TAILQ_NEXT(mp, mnt_list);
796 				vfs_unbusy(mp, td);
797 				continue;
798 			}
799 			sp->f_flags = mp->mnt_flag & MNT_VISFLAGMASK;
800 			error = copyout(sp, sfsp, sizeof(*sp));
801 			if (error) {
802 				vfs_unbusy(mp, td);
803 				return (error);
804 			}
805 			sfsp += sizeof(*sp);
806 		}
807 		count++;
808 		lwkt_gettokref(&ilock);
809 		nmp = TAILQ_NEXT(mp, mnt_list);
810 		vfs_unbusy(mp, td);
811 	}
812 	lwkt_reltoken(&ilock);
813 	if (sfsp && count > maxcount)
814 		uap->sysmsg_result = maxcount;
815 	else
816 		uap->sysmsg_result = count;
817 	return (0);
818 }
819 
820 /*
821  * fchdir_args(int fd)
822  *
823  * Change current working directory to a given file descriptor.
824  */
825 /* ARGSUSED */
826 int
827 fchdir(struct fchdir_args *uap)
828 {
829 	struct thread *td = curthread;
830 	struct proc *p = td->td_proc;
831 	struct filedesc *fdp = p->p_fd;
832 	struct vnode *vp, *ovp;
833 	struct mount *mp;
834 	struct file *fp;
835 	struct namecache *ncp, *oncp;
836 	struct namecache *nct;
837 	int error;
838 
839 	if ((error = getvnode(fdp, SCARG(uap, fd), &fp)) != 0)
840 		return (error);
841 	vp = (struct vnode *)fp->f_data;
842 	vref(vp);
843 	vn_lock(vp, LK_EXCLUSIVE | LK_RETRY, td);
844 	if (vp->v_type != VDIR || fp->f_ncp == NULL)
845 		error = ENOTDIR;
846 	else
847 		error = VOP_ACCESS(vp, VEXEC, p->p_ucred, td);
848 	if (error) {
849 		vput(vp);
850 		return (error);
851 	}
852 	ncp = cache_hold(fp->f_ncp);
853 	while (!error && (mp = vp->v_mountedhere) != NULL) {
854 		error = nlookup_mp(mp, &nct);
855 		if (error == 0) {
856 			cache_unlock(nct);	/* leave ref intact */
857 			vput(vp);
858 			vp = nct->nc_vp;
859 			error = vget(vp, LK_SHARED, td);
860 			KKASSERT(error == 0);
861 			cache_drop(ncp);
862 			ncp = nct;
863 		}
864 	}
865 	if (error == 0) {
866 		ovp = fdp->fd_cdir;
867 		oncp = fdp->fd_ncdir;
868 		VOP_UNLOCK(vp, 0, td);	/* leave ref intact */
869 		fdp->fd_cdir = vp;
870 		fdp->fd_ncdir = ncp;
871 		cache_drop(oncp);
872 		vrele(ovp);
873 	} else {
874 		cache_drop(ncp);
875 		vput(vp);
876 	}
877 	return (error);
878 }
879 
880 int
881 kern_chdir(struct nlookupdata *nd)
882 {
883 	struct thread *td = curthread;
884 	struct proc *p = td->td_proc;
885 	struct filedesc *fdp = p->p_fd;
886 	struct vnode *vp, *ovp;
887 	struct namecache *oncp;
888 	int error;
889 
890 	if ((error = nlookup(nd)) != 0)
891 		return (error);
892 	if ((vp = nd->nl_ncp->nc_vp) == NULL)
893 		return (ENOENT);
894 	if ((error = vget(vp, LK_SHARED, td)) != 0)
895 		return (error);
896 
897 	error = checkvp_chdir(vp, td);
898 	VOP_UNLOCK(vp, 0, td);
899 	if (error == 0) {
900 		ovp = fdp->fd_cdir;
901 		oncp = fdp->fd_ncdir;
902 		cache_unlock(nd->nl_ncp);	/* leave reference intact */
903 		fdp->fd_ncdir = nd->nl_ncp;
904 		fdp->fd_cdir = vp;
905 		cache_drop(oncp);
906 		vrele(ovp);
907 		nd->nl_ncp = NULL;
908 	} else {
909 		vrele(vp);
910 	}
911 	return (error);
912 }
913 
914 /*
915  * chdir_args(char *path)
916  *
917  * Change current working directory (``.'').
918  */
919 int
920 chdir(struct chdir_args *uap)
921 {
922 	struct nlookupdata nd;
923 	int error;
924 
925 	error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
926 	if (error == 0)
927 		error = kern_chdir(&nd);
928 	nlookup_done(&nd);
929 	return (error);
930 }
931 
932 /*
933  * Helper function for raised chroot(2) security function:  Refuse if
934  * any filedescriptors are open directories.
935  */
936 static int
937 chroot_refuse_vdir_fds(fdp)
938 	struct filedesc *fdp;
939 {
940 	struct vnode *vp;
941 	struct file *fp;
942 	int error;
943 	int fd;
944 
945 	for (fd = 0; fd < fdp->fd_nfiles ; fd++) {
946 		error = getvnode(fdp, fd, &fp);
947 		if (error)
948 			continue;
949 		vp = (struct vnode *)fp->f_data;
950 		if (vp->v_type != VDIR)
951 			continue;
952 		return(EPERM);
953 	}
954 	return (0);
955 }
956 
957 /*
958  * This sysctl determines if we will allow a process to chroot(2) if it
959  * has a directory open:
960  *	0: disallowed for all processes.
961  *	1: allowed for processes that were not already chroot(2)'ed.
962  *	2: allowed for all processes.
963  */
964 
965 static int chroot_allow_open_directories = 1;
966 
967 SYSCTL_INT(_kern, OID_AUTO, chroot_allow_open_directories, CTLFLAG_RW,
968      &chroot_allow_open_directories, 0, "");
969 
970 /*
971  * chroot to the specified namecache entry.  We obtain the vp from the
972  * namecache data.  The passed ncp must be locked and referenced and will
973  * remain locked and referenced on return.
974  */
975 static int
976 kern_chroot(struct nlookupdata *nd)
977 {
978 	struct thread *td = curthread;
979 	struct proc *p = td->td_proc;
980 	struct filedesc *fdp = p->p_fd;
981 	struct namecache *ncp;
982 	struct vnode *vp;
983 	int error;
984 
985 	/*
986 	 * Only root can chroot
987 	 */
988 	if ((error = suser_cred(p->p_ucred, PRISON_ROOT)) != 0)
989 		return (error);
990 
991 	if ((error = nlookup(nd)) != 0)
992 		return (error);
993 	ncp = nd->nl_ncp;
994 
995 	/*
996 	 * Disallow open directory descriptors (fchdir() breakouts).
997 	 */
998 	if (chroot_allow_open_directories == 0 ||
999 	   (chroot_allow_open_directories == 1 && fdp->fd_rdir != rootvnode)) {
1000 		if ((error = chroot_refuse_vdir_fds(fdp)) != 0)
1001 			return (error);
1002 	}
1003 	if ((vp = ncp->nc_vp) == NULL)
1004 		return (ENOENT);
1005 
1006 	if ((error = vget(vp, LK_SHARED, td)) != 0)
1007 		return (error);
1008 
1009 	/*
1010 	 * Check the validity of vp as a directory to change to and
1011 	 * associate it with rdir/jdir.
1012 	 */
1013 	error = checkvp_chdir(vp, td);
1014 	VOP_UNLOCK(vp, 0, td);	/* leave reference intact */
1015 	if (error == 0) {
1016 		vrele(fdp->fd_rdir);
1017 		fdp->fd_rdir = vp;	/* reference inherited by fd_rdir */
1018 		cache_drop(fdp->fd_nrdir);
1019 		fdp->fd_nrdir = cache_hold(ncp);
1020 		if (fdp->fd_jdir == NULL) {
1021 			fdp->fd_jdir = vp;
1022 			vref(fdp->fd_jdir);
1023 			fdp->fd_njdir = cache_hold(ncp);
1024 		}
1025 	} else {
1026 		vrele(vp);
1027 	}
1028 	return (error);
1029 }
1030 
1031 /*
1032  * chroot_args(char *path)
1033  *
1034  * Change notion of root (``/'') directory.
1035  */
1036 /* ARGSUSED */
1037 int
1038 chroot(struct chroot_args *uap)
1039 {
1040 	struct thread *td = curthread;
1041 	struct nlookupdata nd;
1042 	int error;
1043 
1044 	KKASSERT(td->td_proc);
1045 	error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
1046 	if (error == 0)
1047 		error = kern_chroot(&nd);
1048 	nlookup_done(&nd);
1049 	return (error);
1050 }
1051 
1052 /*
1053  * Common routine for chroot and chdir.  Given a locked, referenced vnode,
1054  * determine whether it is legal to chdir to the vnode.  The vnode's state
1055  * is not changed by this call.
1056  */
1057 int
1058 checkvp_chdir(struct vnode *vp, struct thread *td)
1059 {
1060 	int error;
1061 
1062 	if (vp->v_type != VDIR)
1063 		error = ENOTDIR;
1064 	else
1065 		error = VOP_ACCESS(vp, VEXEC, td->td_proc->p_ucred, td);
1066 	return (error);
1067 }
1068 
1069 int
1070 kern_open(struct nlookupdata *nd, int oflags, int mode, int *res)
1071 {
1072 	struct thread *td = curthread;
1073 	struct proc *p = td->td_proc;
1074 	struct filedesc *fdp = p->p_fd;
1075 	int cmode, flags;
1076 	struct file *nfp;
1077 	struct file *fp;
1078 	struct vnode *vp;
1079 	int type, indx, error;
1080 	struct flock lf;
1081 
1082 	if ((oflags & O_ACCMODE) == O_ACCMODE)
1083 		return (EINVAL);
1084 	flags = FFLAGS(oflags);
1085 	error = falloc(p, &nfp, NULL);
1086 	if (error)
1087 		return (error);
1088 	fp = nfp;
1089 	cmode = ((mode &~ fdp->fd_cmask) & ALLPERMS) &~ S_ISTXT;
1090 
1091 	/*
1092 	 * XXX p_dupfd is a real mess.  It allows a device to return a
1093 	 * file descriptor to be duplicated rather then doing the open
1094 	 * itself.
1095 	 */
1096 	p->p_dupfd = -1;
1097 
1098 	/*
1099 	 * Call vn_open() to do the lookup and assign the vnode to the
1100 	 * file pointer.  vn_open() does not change the ref count on fp
1101 	 * and the vnode, on success, will be inherited by the file pointer
1102 	 * and unlocked.
1103 	 */
1104 	nd->nl_flags |= NLC_LOCKVP;
1105 	error = vn_open(nd, fp, flags, cmode);
1106 	nlookup_done(nd);
1107 	if (error) {
1108 		/*
1109 		 * handle special fdopen() case.  bleh.  dupfdopen() is
1110 		 * responsible for dropping the old contents of ofiles[indx]
1111 		 * if it succeeds.
1112 		 *
1113 		 * Note that if fsetfd() succeeds it will add a ref to fp
1114 		 * which represents the fd_ofiles[] assignment.  We must still
1115 		 * drop our reference.
1116 		 */
1117 		if ((error == ENODEV || error == ENXIO) && p->p_dupfd >= 0) {
1118 			if (fsetfd(p, fp, &indx) == 0) {
1119 				error = dupfdopen(fdp, indx, p->p_dupfd, flags, error);
1120 				if (error == 0) {
1121 					*res = indx;
1122 					fdrop(fp, td);	/* our ref */
1123 					return (0);
1124 				}
1125 				if (fdp->fd_ofiles[indx] == fp) {
1126 					fdp->fd_ofiles[indx] = NULL;
1127 					fdrop(fp, td);	/* fd_ofiles[] ref */
1128 				}
1129 			}
1130 		}
1131 		fdrop(fp, td);	/* our ref */
1132 		if (error == ERESTART)
1133 			error = EINTR;
1134 		return (error);
1135 	}
1136 
1137 	/*
1138 	 * ref the vnode for ourselves so it can't be ripped out from under
1139 	 * is.  XXX need an ND flag to request that the vnode be returned
1140 	 * anyway.
1141 	 */
1142 	vp = (struct vnode *)fp->f_data;
1143 	vref(vp);
1144 	if ((error = fsetfd(p, fp, &indx)) != 0) {
1145 		fdrop(fp, td);
1146 		vrele(vp);
1147 		return (error);
1148 	}
1149 
1150 	/*
1151 	 * If no error occurs the vp will have been assigned to the file
1152 	 * pointer.
1153 	 */
1154 	p->p_dupfd = 0;
1155 
1156 	/*
1157 	 * There should be 2 references on the file, one from the descriptor
1158 	 * table, and one for us.
1159 	 *
1160 	 * Handle the case where someone closed the file (via its file
1161 	 * descriptor) while we were blocked.  The end result should look
1162 	 * like opening the file succeeded but it was immediately closed.
1163 	 */
1164 	if (fp->f_count == 1) {
1165 		KASSERT(fdp->fd_ofiles[indx] != fp,
1166 		    ("Open file descriptor lost all refs"));
1167 		vrele(vp);
1168 		fo_close(fp, td);
1169 		fdrop(fp, td);
1170 		*res = indx;
1171 		return 0;
1172 	}
1173 
1174 	if (flags & (O_EXLOCK | O_SHLOCK)) {
1175 		lf.l_whence = SEEK_SET;
1176 		lf.l_start = 0;
1177 		lf.l_len = 0;
1178 		if (flags & O_EXLOCK)
1179 			lf.l_type = F_WRLCK;
1180 		else
1181 			lf.l_type = F_RDLCK;
1182 		type = F_FLOCK;
1183 		if ((flags & FNONBLOCK) == 0)
1184 			type |= F_WAIT;
1185 
1186 		if ((error = VOP_ADVLOCK(vp, (caddr_t)fp, F_SETLK, &lf, type)) != 0) {
1187 			/*
1188 			 * lock request failed.  Normally close the descriptor
1189 			 * but handle the case where someone might have dup()d
1190 			 * it when we weren't looking.  One reference is
1191 			 * owned by the descriptor array, the other by us.
1192 			 */
1193 			vrele(vp);
1194 			if (fdp->fd_ofiles[indx] == fp) {
1195 				fdp->fd_ofiles[indx] = NULL;
1196 				fdrop(fp, td);
1197 			}
1198 			fdrop(fp, td);
1199 			return (error);
1200 		}
1201 		fp->f_flag |= FHASLOCK;
1202 	}
1203 	/* assert that vn_open created a backing object if one is needed */
1204 	KASSERT(!vn_canvmio(vp) || VOP_GETVOBJECT(vp, NULL) == 0,
1205 		("open: vmio vnode has no backing object after vn_open"));
1206 
1207 	vrele(vp);
1208 
1209 	/*
1210 	 * release our private reference, leaving the one associated with the
1211 	 * descriptor table intact.
1212 	 */
1213 	fdrop(fp, td);
1214 	*res = indx;
1215 	return (0);
1216 }
1217 
1218 /*
1219  * open_args(char *path, int flags, int mode)
1220  *
1221  * Check permissions, allocate an open file structure,
1222  * and call the device open routine if any.
1223  */
1224 int
1225 open(struct open_args *uap)
1226 {
1227 	struct nlookupdata nd;
1228 	int error;
1229 
1230 	error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
1231 	if (error == 0) {
1232 		error = kern_open(&nd, uap->flags,
1233 				    uap->mode, &uap->sysmsg_result);
1234 	}
1235 	nlookup_done(&nd);
1236 	return (error);
1237 }
1238 
1239 int
1240 kern_mknod(struct nlookupdata *nd, int mode, int dev)
1241 {
1242 	struct namecache *ncp;
1243 	struct thread *td = curthread;
1244 	struct proc *p = td->td_proc;
1245 	struct vnode *vp;
1246 	struct vattr vattr;
1247 	int error;
1248 	int whiteout = 0;
1249 
1250 	KKASSERT(p);
1251 
1252 	switch (mode & S_IFMT) {
1253 	case S_IFCHR:
1254 	case S_IFBLK:
1255 		error = suser(td);
1256 		break;
1257 	default:
1258 		error = suser_cred(p->p_ucred, PRISON_ROOT);
1259 		break;
1260 	}
1261 	if (error)
1262 		return (error);
1263 
1264 	bwillwrite();
1265 	nd->nl_flags |= NLC_CREATE;
1266 	if ((error = nlookup(nd)) != 0)
1267 		return (error);
1268 	ncp = nd->nl_ncp;
1269 	if (ncp->nc_vp)
1270 		return (EEXIST);
1271 
1272 	VATTR_NULL(&vattr);
1273 	vattr.va_mode = (mode & ALLPERMS) &~ p->p_fd->fd_cmask;
1274 	vattr.va_rdev = dev;
1275 	whiteout = 0;
1276 
1277 	switch (mode & S_IFMT) {
1278 	case S_IFMT:	/* used by badsect to flag bad sectors */
1279 		vattr.va_type = VBAD;
1280 		break;
1281 	case S_IFCHR:
1282 		vattr.va_type = VCHR;
1283 		break;
1284 	case S_IFBLK:
1285 		vattr.va_type = VBLK;
1286 		break;
1287 	case S_IFWHT:
1288 		whiteout = 1;
1289 		break;
1290 	default:
1291 		error = EINVAL;
1292 		break;
1293 	}
1294 	if (error == 0) {
1295 		if (whiteout) {
1296 			error = VOP_NWHITEOUT(ncp, nd->nl_cred, NAMEI_CREATE);
1297 		} else {
1298 			vp = NULL;
1299 			error = VOP_NMKNOD(ncp, &vp, nd->nl_cred, &vattr);
1300 			if (error == 0)
1301 				vput(vp);
1302 		}
1303 	}
1304 	return (error);
1305 }
1306 
1307 /*
1308  * mknod_args(char *path, int mode, int dev)
1309  *
1310  * Create a special file.
1311  */
1312 int
1313 mknod(struct mknod_args *uap)
1314 {
1315 	struct nlookupdata nd;
1316 	int error;
1317 
1318 	error = nlookup_init(&nd, uap->path, UIO_USERSPACE, 0);
1319 	if (error == 0)
1320 		error = kern_mknod(&nd, uap->mode, uap->dev);
1321 	nlookup_done(&nd);
1322 	return (error);
1323 }
1324 
1325 int
1326 kern_mkfifo(struct nlookupdata *nd, int mode)
1327 {
1328 	struct namecache *ncp;
1329 	struct thread *td = curthread;
1330 	struct proc *p = td->td_proc;
1331 	struct vattr vattr;
1332 	struct vnode *vp;
1333 	int error;
1334 
1335 	bwillwrite();
1336 
1337 	nd->nl_flags |= NLC_CREATE;
1338 	if ((error = nlookup(nd)) != 0)
1339 		return (error);
1340 	ncp = nd->nl_ncp;
1341 	if (ncp->nc_vp)
1342 		return (EEXIST);
1343 
1344 	VATTR_NULL(&vattr);
1345 	vattr.va_type = VFIFO;
1346 	vattr.va_mode = (mode & ALLPERMS) &~ p->p_fd->fd_cmask;
1347 	vp = NULL;
1348 	error = VOP_NMKNOD(ncp, &vp, nd->nl_cred, &vattr);
1349 	if (error == 0)
1350 		vput(vp);
1351 	return (error);
1352 }
1353 
1354 /*
1355  * mkfifo_args(char *path, int mode)
1356  *
1357  * Create a named pipe.
1358  */
1359 int
1360 mkfifo(struct mkfifo_args *uap)
1361 {
1362 	struct nlookupdata nd;
1363 	int error;
1364 
1365 	error = nlookup_init(&nd, uap->path, UIO_USERSPACE, 0);
1366 	if (error == 0)
1367 		error = kern_mkfifo(&nd, uap->mode);
1368 	nlookup_done(&nd);
1369 	return (error);
1370 }
1371 
1372 int
1373 kern_link(struct nlookupdata *nd, struct nlookupdata *linknd)
1374 {
1375 	struct thread *td = curthread;
1376 	struct vnode *vp;
1377 	int error;
1378 
1379 	/*
1380 	 * Lookup the source and obtained a locked vnode.
1381 	 *
1382 	 * XXX relookup on vget failure / race ?
1383 	 */
1384 	bwillwrite();
1385 	if ((error = nlookup(nd)) != 0)
1386 		return (error);
1387 	vp = nd->nl_ncp->nc_vp;
1388 	KKASSERT(vp != NULL);
1389 	if (vp->v_type == VDIR)
1390 		return (EPERM);		/* POSIX */
1391 	if ((error = vget(vp, LK_EXCLUSIVE, td)) != 0)
1392 		return (error);
1393 
1394 	/*
1395 	 * Unlock the source so we can lookup the target without deadlocking
1396 	 * (XXX vp is locked already, possible other deadlock?).  The target
1397 	 * must not exist.
1398 	 */
1399 	KKASSERT(nd->nl_flags & NLC_NCPISLOCKED);
1400 	nd->nl_flags &= ~NLC_NCPISLOCKED;
1401 	cache_unlock(nd->nl_ncp);
1402 
1403 	linknd->nl_flags |= NLC_CREATE;
1404 	if ((error = nlookup(linknd)) != 0) {
1405 		vput(vp);
1406 		return (error);
1407 	}
1408 	if (linknd->nl_ncp->nc_vp) {
1409 		vput(vp);
1410 		return (EEXIST);
1411 	}
1412 
1413 	/*
1414 	 * Finally run the new API VOP.
1415 	 */
1416 	error = VOP_NLINK(linknd->nl_ncp, vp, linknd->nl_cred);
1417 	vput(vp);
1418 	return (error);
1419 }
1420 
1421 /*
1422  * link_args(char *path, char *link)
1423  *
1424  * Make a hard file link.
1425  */
1426 int
1427 link(struct link_args *uap)
1428 {
1429 	struct nlookupdata nd, linknd;
1430 	int error;
1431 
1432 	error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
1433 	if (error == 0) {
1434 		error = nlookup_init(&linknd, uap->link, UIO_USERSPACE, 0);
1435 		if (error == 0)
1436 			error = kern_link(&nd, &linknd);
1437 		nlookup_done(&linknd);
1438 	}
1439 	nlookup_done(&nd);
1440 	return (error);
1441 }
1442 
1443 int
1444 kern_symlink(struct nlookupdata *nd, char *path, int mode)
1445 {
1446 	struct namecache *ncp;
1447 	struct vattr vattr;
1448 	struct vnode *vp;
1449 	int error;
1450 
1451 	bwillwrite();
1452 	nd->nl_flags |= NLC_CREATE;
1453 	if ((error = nlookup(nd)) != 0)
1454 		return (error);
1455 	ncp = nd->nl_ncp;
1456 	if (ncp->nc_vp)
1457 		return (EEXIST);
1458 
1459 	VATTR_NULL(&vattr);
1460 	vattr.va_mode = mode;
1461 	error = VOP_NSYMLINK(ncp, &vp, nd->nl_cred, &vattr, path);
1462 	if (error == 0)
1463 		vput(vp);
1464 	return (error);
1465 }
1466 
1467 /*
1468  * symlink(char *path, char *link)
1469  *
1470  * Make a symbolic link.
1471  */
1472 int
1473 symlink(struct symlink_args *uap)
1474 {
1475 	struct thread *td = curthread;
1476 	struct nlookupdata nd;
1477 	char *path;
1478 	int error;
1479 	int mode;
1480 
1481 	path = zalloc(namei_zone);
1482 	error = copyinstr(uap->path, path, MAXPATHLEN, NULL);
1483 	if (error == 0) {
1484 		error = nlookup_init(&nd, uap->link, UIO_USERSPACE, 0);
1485 		if (error == 0) {
1486 			mode = ACCESSPERMS & ~td->td_proc->p_fd->fd_cmask;
1487 			error = kern_symlink(&nd, path, mode);
1488 		}
1489 		nlookup_done(&nd);
1490 	}
1491 	zfree(namei_zone, path);
1492 	return (error);
1493 }
1494 
1495 /*
1496  * undelete_args(char *path)
1497  *
1498  * Delete a whiteout from the filesystem.
1499  */
1500 /* ARGSUSED */
1501 int
1502 undelete(struct undelete_args *uap)
1503 {
1504 	struct nlookupdata nd;
1505 	int error;
1506 
1507 	error = nlookup_init(&nd, SCARG(uap, path), UIO_USERSPACE, 0);
1508 	bwillwrite();
1509 	nd.nl_flags |= NLC_DELETE;
1510 	if (error == 0)
1511 		error = nlookup(&nd);
1512 	if (error == 0)
1513 		error = VOP_NWHITEOUT(nd.nl_ncp, nd.nl_cred, NAMEI_DELETE);
1514 	nlookup_done(&nd);
1515 	return (error);
1516 }
1517 
1518 int
1519 kern_unlink(struct nlookupdata *nd)
1520 {
1521 	struct namecache *ncp;
1522 	int error;
1523 
1524 	bwillwrite();
1525 	nd->nl_flags |= NLC_DELETE;
1526 	if ((error = nlookup(nd)) != 0)
1527 		return (error);
1528 	ncp = nd->nl_ncp;
1529 	error = VOP_NREMOVE(ncp, nd->nl_cred);
1530 	return (error);
1531 }
1532 
1533 /*
1534  * unlink_args(char *path)
1535  *
1536  * Delete a name from the filesystem.
1537  */
1538 int
1539 unlink(struct unlink_args *uap)
1540 {
1541 	struct nlookupdata nd;
1542 	int error;
1543 
1544 	error = nlookup_init(&nd, uap->path, UIO_USERSPACE, 0);
1545 	if (error == 0)
1546 		error = kern_unlink(&nd);
1547 	nlookup_done(&nd);
1548 	return (error);
1549 }
1550 
1551 int
1552 kern_lseek(int fd, off_t offset, int whence, off_t *res)
1553 {
1554 	struct thread *td = curthread;
1555 	struct proc *p = td->td_proc;
1556 	struct filedesc *fdp = p->p_fd;
1557 	struct file *fp;
1558 	struct vattr vattr;
1559 	int error;
1560 
1561 	if (fd >= fdp->fd_nfiles ||
1562 	    (fp = fdp->fd_ofiles[fd]) == NULL)
1563 		return (EBADF);
1564 	if (fp->f_type != DTYPE_VNODE)
1565 		return (ESPIPE);
1566 	switch (whence) {
1567 	case L_INCR:
1568 		fp->f_offset += offset;
1569 		break;
1570 	case L_XTND:
1571 		error=VOP_GETATTR((struct vnode *)fp->f_data, &vattr, td);
1572 		if (error)
1573 			return (error);
1574 		fp->f_offset = offset + vattr.va_size;
1575 		break;
1576 	case L_SET:
1577 		fp->f_offset = offset;
1578 		break;
1579 	default:
1580 		return (EINVAL);
1581 	}
1582 	*res = fp->f_offset;
1583 	return (0);
1584 }
1585 
1586 /*
1587  * lseek_args(int fd, int pad, off_t offset, int whence)
1588  *
1589  * Reposition read/write file offset.
1590  */
1591 int
1592 lseek(struct lseek_args *uap)
1593 {
1594 	int error;
1595 
1596 	error = kern_lseek(uap->fd, uap->offset, uap->whence,
1597 	    &uap->sysmsg_offset);
1598 
1599 	return (error);
1600 }
1601 
1602 int
1603 kern_access(struct nlookupdata *nd, int aflags)
1604 {
1605 	struct thread *td = curthread;
1606 	struct vnode *vp;
1607 	int error, flags;
1608 
1609 	if ((error = nlookup(nd)) != 0)
1610 		return (error);
1611 retry:
1612 	error = cache_vget(nd->nl_ncp, nd->nl_cred, LK_EXCLUSIVE, &vp);
1613 	if (error)
1614 		return (error);
1615 
1616 	/* Flags == 0 means only check for existence. */
1617 	if (aflags) {
1618 		flags = 0;
1619 		if (aflags & R_OK)
1620 			flags |= VREAD;
1621 		if (aflags & W_OK)
1622 			flags |= VWRITE;
1623 		if (aflags & X_OK)
1624 			flags |= VEXEC;
1625 		if ((flags & VWRITE) == 0 || (error = vn_writechk(vp)) == 0)
1626 			error = VOP_ACCESS(vp, flags, nd->nl_cred, td);
1627 
1628 		/*
1629 		 * If the file handle is stale we have to re-resolve the
1630 		 * entry.  This is a hack at the moment.
1631 		 */
1632 		if (error == ESTALE) {
1633 			cache_setunresolved(nd->nl_ncp);
1634 			error = cache_resolve(nd->nl_ncp, nd->nl_cred);
1635 			if (error == 0) {
1636 				vput(vp);
1637 				vp = NULL;
1638 				goto retry;
1639 			}
1640 		}
1641 	}
1642 	vput(vp);
1643 	return (error);
1644 }
1645 
1646 /*
1647  * access_args(char *path, int flags)
1648  *
1649  * Check access permissions.
1650  */
1651 int
1652 access(struct access_args *uap)
1653 {
1654 	struct nlookupdata nd;
1655 	int error;
1656 
1657 	error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
1658 	if (error == 0)
1659 		error = kern_access(&nd, uap->flags);
1660 	nlookup_done(&nd);
1661 	return (error);
1662 }
1663 
1664 int
1665 kern_stat(struct nlookupdata *nd, struct stat *st)
1666 {
1667 	int error;
1668 	struct vnode *vp;
1669 	thread_t td;
1670 
1671 	if ((error = nlookup(nd)) != 0)
1672 		return (error);
1673 again:
1674 	if ((vp = nd->nl_ncp->nc_vp) == NULL)
1675 		return (ENOENT);
1676 
1677 	td = curthread;
1678 	if ((error = vget(vp, LK_SHARED, td)) != 0)
1679 		return (error);
1680 	error = vn_stat(vp, st, td);
1681 
1682 	/*
1683 	 * If the file handle is stale we have to re-resolve the entry.  This
1684 	 * is a hack at the moment.
1685 	 */
1686 	if (error == ESTALE) {
1687 		cache_setunresolved(nd->nl_ncp);
1688 		error = cache_resolve(nd->nl_ncp, nd->nl_cred);
1689 		if (error == 0) {
1690 			vput(vp);
1691 			goto again;
1692 		}
1693 	}
1694 	vput(vp);
1695 	return (error);
1696 }
1697 
1698 /*
1699  * stat_args(char *path, struct stat *ub)
1700  *
1701  * Get file status; this version follows links.
1702  */
1703 int
1704 stat(struct stat_args *uap)
1705 {
1706 	struct nlookupdata nd;
1707 	struct stat st;
1708 	int error;
1709 
1710 	error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
1711 	if (error == 0) {
1712 		error = kern_stat(&nd, &st);
1713 		if (error == 0)
1714 			error = copyout(&st, uap->ub, sizeof(*uap->ub));
1715 	}
1716 	nlookup_done(&nd);
1717 	return (error);
1718 }
1719 
1720 /*
1721  * lstat_args(char *path, struct stat *ub)
1722  *
1723  * Get file status; this version does not follow links.
1724  */
1725 int
1726 lstat(struct lstat_args *uap)
1727 {
1728 	struct nlookupdata nd;
1729 	struct stat st;
1730 	int error;
1731 
1732 	error = nlookup_init(&nd, uap->path, UIO_USERSPACE, 0);
1733 	if (error == 0) {
1734 		error = kern_stat(&nd, &st);
1735 		if (error == 0)
1736 			error = copyout(&st, uap->ub, sizeof(*uap->ub));
1737 	}
1738 	nlookup_done(&nd);
1739 	return (error);
1740 }
1741 
1742 void
1743 cvtnstat(sb, nsb)
1744 	struct stat *sb;
1745 	struct nstat *nsb;
1746 {
1747 	nsb->st_dev = sb->st_dev;
1748 	nsb->st_ino = sb->st_ino;
1749 	nsb->st_mode = sb->st_mode;
1750 	nsb->st_nlink = sb->st_nlink;
1751 	nsb->st_uid = sb->st_uid;
1752 	nsb->st_gid = sb->st_gid;
1753 	nsb->st_rdev = sb->st_rdev;
1754 	nsb->st_atimespec = sb->st_atimespec;
1755 	nsb->st_mtimespec = sb->st_mtimespec;
1756 	nsb->st_ctimespec = sb->st_ctimespec;
1757 	nsb->st_size = sb->st_size;
1758 	nsb->st_blocks = sb->st_blocks;
1759 	nsb->st_blksize = sb->st_blksize;
1760 	nsb->st_flags = sb->st_flags;
1761 	nsb->st_gen = sb->st_gen;
1762 	nsb->st_qspare[0] = sb->st_qspare[0];
1763 	nsb->st_qspare[1] = sb->st_qspare[1];
1764 }
1765 
1766 /*
1767  * nstat_args(char *path, struct nstat *ub)
1768  */
1769 /* ARGSUSED */
1770 int
1771 nstat(struct nstat_args *uap)
1772 {
1773 	struct thread *td = curthread;
1774 	struct vnode *vp;
1775 	struct stat sb;
1776 	struct nstat nsb;
1777 	struct nlookupdata nd;
1778 	int error;
1779 
1780 	vp = NULL;
1781 	error = nlookup_init(&nd, SCARG(uap, path), UIO_USERSPACE, NLC_FOLLOW);
1782 	if (error == 0)
1783 		error = nlookup(&nd);
1784 	if (error == 0)
1785 		error = cache_vget(nd.nl_ncp, nd.nl_cred, LK_EXCLUSIVE, &vp);
1786 	nlookup_done(&nd);
1787 	if (error == 0) {
1788 		error = vn_stat(vp, &sb, td);
1789 		vput(vp);
1790 		if (error == 0) {
1791 			cvtnstat(&sb, &nsb);
1792 			error = copyout(&nsb, SCARG(uap, ub), sizeof(nsb));
1793 		}
1794 	}
1795 	return (error);
1796 }
1797 
1798 /*
1799  * lstat_args(char *path, struct stat *ub)
1800  *
1801  * Get file status; this version does not follow links.
1802  */
1803 /* ARGSUSED */
1804 int
1805 nlstat(struct nlstat_args *uap)
1806 {
1807 	struct thread *td = curthread;
1808 	struct vnode *vp;
1809 	struct stat sb;
1810 	struct nstat nsb;
1811 	struct nlookupdata nd;
1812 	int error;
1813 
1814 	vp = NULL;
1815 	error = nlookup_init(&nd, SCARG(uap, path), UIO_USERSPACE, 0);
1816 	if (error == 0)
1817 		error = nlookup(&nd);
1818 	if (error == 0)
1819 		error = cache_vget(nd.nl_ncp, nd.nl_cred, LK_EXCLUSIVE, &vp);
1820 	nlookup_done(&nd);
1821 	if (error == 0) {
1822 		error = vn_stat(vp, &sb, td);
1823 		vput(vp);
1824 		if (error == 0) {
1825 			cvtnstat(&sb, &nsb);
1826 			error = copyout(&nsb, SCARG(uap, ub), sizeof(nsb));
1827 		}
1828 	}
1829 	return (error);
1830 }
1831 
1832 /*
1833  * pathconf_Args(char *path, int name)
1834  *
1835  * Get configurable pathname variables.
1836  */
1837 /* ARGSUSED */
1838 int
1839 pathconf(struct pathconf_args *uap)
1840 {
1841 	struct nlookupdata nd;
1842 	struct vnode *vp;
1843 	int error;
1844 
1845 	vp = NULL;
1846 	error = nlookup_init(&nd, SCARG(uap, path), UIO_USERSPACE, NLC_FOLLOW);
1847 	if (error == 0)
1848 		error = nlookup(&nd);
1849 	if (error == 0)
1850 		error = cache_vget(nd.nl_ncp, nd.nl_cred, LK_EXCLUSIVE, &vp);
1851 	nlookup_done(&nd);
1852 	if (error == 0) {
1853 		error = VOP_PATHCONF(vp, SCARG(uap, name), uap->sysmsg_fds);
1854 		vput(vp);
1855 	}
1856 	return (error);
1857 }
1858 
1859 /*
1860  * XXX: daver
1861  * kern_readlink isn't properly split yet.  There is a copyin burried
1862  * in VOP_READLINK().
1863  */
1864 int
1865 kern_readlink(struct nlookupdata *nd, char *buf, int count, int *res)
1866 {
1867 	struct thread *td = curthread;
1868 	struct proc *p = td->td_proc;
1869 	struct vnode *vp;
1870 	struct iovec aiov;
1871 	struct uio auio;
1872 	int error;
1873 
1874 	if ((error = nlookup(nd)) != 0)
1875 		return (error);
1876 	error = cache_vget(nd->nl_ncp, nd->nl_cred, LK_EXCLUSIVE, &vp);
1877 	if (error)
1878 		return (error);
1879 	if (vp->v_type != VLNK) {
1880 		error = EINVAL;
1881 	} else {
1882 		aiov.iov_base = buf;
1883 		aiov.iov_len = count;
1884 		auio.uio_iov = &aiov;
1885 		auio.uio_iovcnt = 1;
1886 		auio.uio_offset = 0;
1887 		auio.uio_rw = UIO_READ;
1888 		auio.uio_segflg = UIO_USERSPACE;
1889 		auio.uio_td = td;
1890 		auio.uio_resid = count;
1891 		error = VOP_READLINK(vp, &auio, p->p_ucred);
1892 	}
1893 	vput(vp);
1894 	*res = count - auio.uio_resid;
1895 	return (error);
1896 }
1897 
1898 /*
1899  * readlink_args(char *path, char *buf, int count)
1900  *
1901  * Return target name of a symbolic link.
1902  */
1903 int
1904 readlink(struct readlink_args *uap)
1905 {
1906 	struct nlookupdata nd;
1907 	int error;
1908 
1909 	error = nlookup_init(&nd, uap->path, UIO_USERSPACE, 0);
1910 	if (error == 0) {
1911 		error = kern_readlink(&nd, uap->buf, uap->count,
1912 					&uap->sysmsg_result);
1913 	}
1914 	nlookup_done(&nd);
1915 	return (error);
1916 }
1917 
1918 static int
1919 setfflags(struct vnode *vp, int flags)
1920 {
1921 	struct thread *td = curthread;
1922 	struct proc *p = td->td_proc;
1923 	int error;
1924 	struct vattr vattr;
1925 
1926 	/*
1927 	 * Prevent non-root users from setting flags on devices.  When
1928 	 * a device is reused, users can retain ownership of the device
1929 	 * if they are allowed to set flags and programs assume that
1930 	 * chown can't fail when done as root.
1931 	 */
1932 	if ((vp->v_type == VCHR || vp->v_type == VBLK) &&
1933 	    ((error = suser_cred(p->p_ucred, PRISON_ROOT)) != 0))
1934 		return (error);
1935 
1936 	/*
1937 	 * note: vget is required for any operation that might mod the vnode
1938 	 * so VINACTIVE is properly cleared.
1939 	 */
1940 	VOP_LEASE(vp, td, p->p_ucred, LEASE_WRITE);
1941 	if ((error = vget(vp, LK_EXCLUSIVE, td)) == 0) {
1942 		VATTR_NULL(&vattr);
1943 		vattr.va_flags = flags;
1944 		error = VOP_SETATTR(vp, &vattr, p->p_ucred, td);
1945 		vput(vp);
1946 	}
1947 	return (error);
1948 }
1949 
1950 /*
1951  * chflags(char *path, int flags)
1952  *
1953  * Change flags of a file given a path name.
1954  */
1955 /* ARGSUSED */
1956 int
1957 chflags(struct chflags_args *uap)
1958 {
1959 	struct nlookupdata nd;
1960 	struct vnode *vp;
1961 	int error;
1962 
1963 	vp = NULL;
1964 	error = nlookup_init(&nd, SCARG(uap, path), UIO_USERSPACE, NLC_FOLLOW);
1965 	/* XXX Add NLC flag indicating modifying operation? */
1966 	if (error == 0)
1967 		error = nlookup(&nd);
1968 	if (error == 0)
1969 		error = cache_vref(nd.nl_ncp, nd.nl_cred, &vp);
1970 	nlookup_done(&nd);
1971 	if (error == 0) {
1972 		error = setfflags(vp, SCARG(uap, flags));
1973 		vrele(vp);
1974 	}
1975 	return (error);
1976 }
1977 
1978 /*
1979  * fchflags_args(int fd, int flags)
1980  *
1981  * Change flags of a file given a file descriptor.
1982  */
1983 /* ARGSUSED */
1984 int
1985 fchflags(struct fchflags_args *uap)
1986 {
1987 	struct thread *td = curthread;
1988 	struct proc *p = td->td_proc;
1989 	struct file *fp;
1990 	int error;
1991 
1992 	if ((error = getvnode(p->p_fd, SCARG(uap, fd), &fp)) != 0)
1993 		return (error);
1994 	return setfflags((struct vnode *) fp->f_data, SCARG(uap, flags));
1995 }
1996 
1997 static int
1998 setfmode(struct vnode *vp, int mode)
1999 {
2000 	struct thread *td = curthread;
2001 	struct proc *p = td->td_proc;
2002 	int error;
2003 	struct vattr vattr;
2004 
2005 	/*
2006 	 * note: vget is required for any operation that might mod the vnode
2007 	 * so VINACTIVE is properly cleared.
2008 	 */
2009 	VOP_LEASE(vp, td, p->p_ucred, LEASE_WRITE);
2010 	if ((error = vget(vp, LK_EXCLUSIVE, td)) == 0) {
2011 		VATTR_NULL(&vattr);
2012 		vattr.va_mode = mode & ALLPERMS;
2013 		error = VOP_SETATTR(vp, &vattr, p->p_ucred, td);
2014 		vput(vp);
2015 	}
2016 	return error;
2017 }
2018 
2019 int
2020 kern_chmod(struct nlookupdata *nd, int mode)
2021 {
2022 	struct vnode *vp;
2023 	int error;
2024 
2025 	/* XXX Add NLC flag indicating modifying operation? */
2026 	if ((error = nlookup(nd)) != 0)
2027 		return (error);
2028 	if ((error = cache_vref(nd->nl_ncp, nd->nl_cred, &vp)) != 0)
2029 		return (error);
2030 	error = setfmode(vp, mode);
2031 	vrele(vp);
2032 	return (error);
2033 }
2034 
2035 /*
2036  * chmod_args(char *path, int mode)
2037  *
2038  * Change mode of a file given path name.
2039  */
2040 /* ARGSUSED */
2041 int
2042 chmod(struct chmod_args *uap)
2043 {
2044 	struct nlookupdata nd;
2045 	int error;
2046 
2047 	error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
2048 	if (error == 0)
2049 		error = kern_chmod(&nd, uap->mode);
2050 	nlookup_done(&nd);
2051 	return (error);
2052 }
2053 
2054 /*
2055  * lchmod_args(char *path, int mode)
2056  *
2057  * Change mode of a file given path name (don't follow links.)
2058  */
2059 /* ARGSUSED */
2060 int
2061 lchmod(struct lchmod_args *uap)
2062 {
2063 	struct nlookupdata nd;
2064 	int error;
2065 
2066 	error = nlookup_init(&nd, uap->path, UIO_USERSPACE, 0);
2067 	if (error == 0)
2068 		error = kern_chmod(&nd, uap->mode);
2069 	nlookup_done(&nd);
2070 	return (error);
2071 }
2072 
2073 /*
2074  * fchmod_args(int fd, int mode)
2075  *
2076  * Change mode of a file given a file descriptor.
2077  */
2078 /* ARGSUSED */
2079 int
2080 fchmod(struct fchmod_args *uap)
2081 {
2082 	struct thread *td = curthread;
2083 	struct proc *p = td->td_proc;
2084 	struct file *fp;
2085 	int error;
2086 
2087 	if ((error = getvnode(p->p_fd, SCARG(uap, fd), &fp)) != 0)
2088 		return (error);
2089 	return setfmode((struct vnode *)fp->f_data, SCARG(uap, mode));
2090 }
2091 
2092 static int
2093 setfown(struct vnode *vp, uid_t uid, gid_t gid)
2094 {
2095 	struct thread *td = curthread;
2096 	struct proc *p = td->td_proc;
2097 	int error;
2098 	struct vattr vattr;
2099 
2100 	/*
2101 	 * note: vget is required for any operation that might mod the vnode
2102 	 * so VINACTIVE is properly cleared.
2103 	 */
2104 	VOP_LEASE(vp, td, p->p_ucred, LEASE_WRITE);
2105 	if ((error = vget(vp, LK_EXCLUSIVE, td)) == 0) {
2106 		VATTR_NULL(&vattr);
2107 		vattr.va_uid = uid;
2108 		vattr.va_gid = gid;
2109 		error = VOP_SETATTR(vp, &vattr, p->p_ucred, td);
2110 		vput(vp);
2111 	}
2112 	return error;
2113 }
2114 
2115 int
2116 kern_chown(struct nlookupdata *nd, int uid, int gid)
2117 {
2118 	struct vnode *vp;
2119 	int error;
2120 
2121 	/* XXX Add NLC flag indicating modifying operation? */
2122 	if ((error = nlookup(nd)) != 0)
2123 		return (error);
2124 	if ((error = cache_vref(nd->nl_ncp, nd->nl_cred, &vp)) != 0)
2125 		return (error);
2126 	error = setfown(vp, uid, gid);
2127 	vrele(vp);
2128 	return (error);
2129 }
2130 
2131 /*
2132  * chown(char *path, int uid, int gid)
2133  *
2134  * Set ownership given a path name.
2135  */
2136 int
2137 chown(struct chown_args *uap)
2138 {
2139 	struct nlookupdata nd;
2140 	int error;
2141 
2142 	error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
2143 	if (error == 0)
2144 		error = kern_chown(&nd, uap->uid, uap->gid);
2145 	nlookup_done(&nd);
2146 	return (error);
2147 }
2148 
2149 /*
2150  * lchown_args(char *path, int uid, int gid)
2151  *
2152  * Set ownership given a path name, do not cross symlinks.
2153  */
2154 int
2155 lchown(struct lchown_args *uap)
2156 {
2157 	struct nlookupdata nd;
2158 	int error;
2159 
2160 	error = nlookup_init(&nd, uap->path, UIO_USERSPACE, 0);
2161 	if (error == 0)
2162 		error = kern_chown(&nd, uap->uid, uap->gid);
2163 	nlookup_done(&nd);
2164 	return (error);
2165 }
2166 
2167 /*
2168  * fchown_args(int fd, int uid, int gid)
2169  *
2170  * Set ownership given a file descriptor.
2171  */
2172 /* ARGSUSED */
2173 int
2174 fchown(struct fchown_args *uap)
2175 {
2176 	struct thread *td = curthread;
2177 	struct proc *p = td->td_proc;
2178 	struct file *fp;
2179 	int error;
2180 
2181 	if ((error = getvnode(p->p_fd, SCARG(uap, fd), &fp)) != 0)
2182 		return (error);
2183 	return setfown((struct vnode *)fp->f_data,
2184 		SCARG(uap, uid), SCARG(uap, gid));
2185 }
2186 
2187 static int
2188 getutimes(const struct timeval *tvp, struct timespec *tsp)
2189 {
2190 	struct timeval tv[2];
2191 
2192 	if (tvp == NULL) {
2193 		microtime(&tv[0]);
2194 		TIMEVAL_TO_TIMESPEC(&tv[0], &tsp[0]);
2195 		tsp[1] = tsp[0];
2196 	} else {
2197 		TIMEVAL_TO_TIMESPEC(&tvp[0], &tsp[0]);
2198 		TIMEVAL_TO_TIMESPEC(&tvp[1], &tsp[1]);
2199 	}
2200 	return 0;
2201 }
2202 
2203 static int
2204 setutimes(struct vnode *vp, const struct timespec *ts, int nullflag)
2205 {
2206 	struct thread *td = curthread;
2207 	struct proc *p = td->td_proc;
2208 	int error;
2209 	struct vattr vattr;
2210 
2211 	/*
2212 	 * note: vget is required for any operation that might mod the vnode
2213 	 * so VINACTIVE is properly cleared.
2214 	 */
2215 	VOP_LEASE(vp, td, p->p_ucred, LEASE_WRITE);
2216 	if ((error = vget(vp, LK_EXCLUSIVE, td)) == 0) {
2217 		VATTR_NULL(&vattr);
2218 		vattr.va_atime = ts[0];
2219 		vattr.va_mtime = ts[1];
2220 		if (nullflag)
2221 			vattr.va_vaflags |= VA_UTIMES_NULL;
2222 		error = VOP_SETATTR(vp, &vattr, p->p_ucred, td);
2223 		vput(vp);
2224 	}
2225 	return error;
2226 }
2227 
2228 int
2229 kern_utimes(struct nlookupdata *nd, struct timeval *tptr)
2230 {
2231 	struct timespec ts[2];
2232 	struct vnode *vp;
2233 	int error;
2234 
2235 	if ((error = getutimes(tptr, ts)) != 0)
2236 		return (error);
2237 	/* XXX Add NLC flag indicating modifying operation? */
2238 	if ((error = nlookup(nd)) != 0)
2239 		return (error);
2240 	if ((error = cache_vref(nd->nl_ncp, nd->nl_cred, &vp)) != 0)
2241 		return (error);
2242 	error = setutimes(vp, ts, tptr == NULL);
2243 	vrele(vp);
2244 	return (error);
2245 }
2246 
2247 /*
2248  * utimes_args(char *path, struct timeval *tptr)
2249  *
2250  * Set the access and modification times of a file.
2251  */
2252 int
2253 utimes(struct utimes_args *uap)
2254 {
2255 	struct timeval tv[2];
2256 	struct nlookupdata nd;
2257 	int error;
2258 
2259 	if (uap->tptr) {
2260  		error = copyin(uap->tptr, tv, sizeof(tv));
2261 		if (error)
2262 			return (error);
2263 	}
2264 	error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
2265 	if (error == 0)
2266 		error = kern_utimes(&nd, uap->tptr ? tv : NULL);
2267 	nlookup_done(&nd);
2268 	return (error);
2269 }
2270 
2271 /*
2272  * lutimes_args(char *path, struct timeval *tptr)
2273  *
2274  * Set the access and modification times of a file.
2275  */
2276 int
2277 lutimes(struct lutimes_args *uap)
2278 {
2279 	struct timeval tv[2];
2280 	struct nlookupdata nd;
2281 	int error;
2282 
2283 	if (uap->tptr) {
2284 		error = copyin(uap->tptr, tv, sizeof(tv));
2285 		if (error)
2286 			return (error);
2287 	}
2288 	error = nlookup_init(&nd, uap->path, UIO_USERSPACE, 0);
2289 	if (error == 0)
2290 		error = kern_utimes(&nd, uap->tptr ? tv : NULL);
2291 	nlookup_done(&nd);
2292 	return (error);
2293 }
2294 
2295 int
2296 kern_futimes(int fd, struct timeval *tptr)
2297 {
2298 	struct thread *td = curthread;
2299 	struct proc *p = td->td_proc;
2300 	struct timespec ts[2];
2301 	struct file *fp;
2302 	int error;
2303 
2304 	error = getutimes(tptr, ts);
2305 	if (error)
2306 		return (error);
2307 	error = getvnode(p->p_fd, fd, &fp);
2308 	if (error)
2309 		return (error);
2310 	error =  setutimes((struct vnode *)fp->f_data, ts, tptr == NULL);
2311 	return (error);
2312 }
2313 
2314 /*
2315  * futimes_args(int fd, struct timeval *tptr)
2316  *
2317  * Set the access and modification times of a file.
2318  */
2319 int
2320 futimes(struct futimes_args *uap)
2321 {
2322 	struct timeval tv[2];
2323 	int error;
2324 
2325 	if (uap->tptr) {
2326 		error = copyin(uap->tptr, tv, sizeof(tv));
2327 		if (error)
2328 			return (error);
2329 	}
2330 
2331 	error = kern_futimes(uap->fd, uap->tptr ? tv : NULL);
2332 
2333 	return (error);
2334 }
2335 
2336 int
2337 kern_truncate(struct nlookupdata *nd, off_t length)
2338 {
2339 	struct vnode *vp;
2340 	struct vattr vattr;
2341 	int error;
2342 
2343 	if (length < 0)
2344 		return(EINVAL);
2345 	/* XXX Add NLC flag indicating modifying operation? */
2346 	if ((error = nlookup(nd)) != 0)
2347 		return (error);
2348 	if ((error = cache_vref(nd->nl_ncp, nd->nl_cred, &vp)) != 0)
2349 		return (error);
2350 	VOP_LEASE(vp, nd->nl_td, nd->nl_cred, LEASE_WRITE);
2351 	if ((error = vn_lock(vp, LK_EXCLUSIVE | LK_RETRY, nd->nl_td)) != 0) {
2352 		vrele(vp);
2353 		return (error);
2354 	}
2355 	if (vp->v_type == VDIR) {
2356 		error = EISDIR;
2357 	} else if ((error = vn_writechk(vp)) == 0 &&
2358 	    (error = VOP_ACCESS(vp, VWRITE, nd->nl_cred, nd->nl_td)) == 0) {
2359 		VATTR_NULL(&vattr);
2360 		vattr.va_size = length;
2361 		error = VOP_SETATTR(vp, &vattr, nd->nl_cred, nd->nl_td);
2362 	}
2363 	vput(vp);
2364 	return (error);
2365 }
2366 
2367 /*
2368  * truncate(char *path, int pad, off_t length)
2369  *
2370  * Truncate a file given its path name.
2371  */
2372 int
2373 truncate(struct truncate_args *uap)
2374 {
2375 	struct nlookupdata nd;
2376 	int error;
2377 
2378 	error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
2379 	if (error == 0)
2380 		error = kern_truncate(&nd, uap->length);
2381 	nlookup_done(&nd);
2382 	return error;
2383 }
2384 
2385 int
2386 kern_ftruncate(int fd, off_t length)
2387 {
2388 	struct thread *td = curthread;
2389 	struct proc *p = td->td_proc;
2390 	struct vattr vattr;
2391 	struct vnode *vp;
2392 	struct file *fp;
2393 	int error;
2394 
2395 	if (length < 0)
2396 		return(EINVAL);
2397 	if ((error = getvnode(p->p_fd, fd, &fp)) != 0)
2398 		return (error);
2399 	if ((fp->f_flag & FWRITE) == 0)
2400 		return (EINVAL);
2401 	vp = (struct vnode *)fp->f_data;
2402 	VOP_LEASE(vp, td, p->p_ucred, LEASE_WRITE);
2403 	vn_lock(vp, LK_EXCLUSIVE | LK_RETRY, td);
2404 	if (vp->v_type == VDIR)
2405 		error = EISDIR;
2406 	else if ((error = vn_writechk(vp)) == 0) {
2407 		VATTR_NULL(&vattr);
2408 		vattr.va_size = length;
2409 		error = VOP_SETATTR(vp, &vattr, fp->f_cred, td);
2410 	}
2411 	VOP_UNLOCK(vp, 0, td);
2412 	return (error);
2413 }
2414 
2415 /*
2416  * ftruncate_args(int fd, int pad, off_t length)
2417  *
2418  * Truncate a file given a file descriptor.
2419  */
2420 int
2421 ftruncate(struct ftruncate_args *uap)
2422 {
2423 	int error;
2424 
2425 	error = kern_ftruncate(uap->fd, uap->length);
2426 
2427 	return (error);
2428 }
2429 
2430 /*
2431  * fsync(int fd)
2432  *
2433  * Sync an open file.
2434  */
2435 /* ARGSUSED */
2436 int
2437 fsync(struct fsync_args *uap)
2438 {
2439 	struct thread *td = curthread;
2440 	struct proc *p = td->td_proc;
2441 	struct vnode *vp;
2442 	struct file *fp;
2443 	vm_object_t obj;
2444 	int error;
2445 
2446 	if ((error = getvnode(p->p_fd, SCARG(uap, fd), &fp)) != 0)
2447 		return (error);
2448 	vp = (struct vnode *)fp->f_data;
2449 	vn_lock(vp, LK_EXCLUSIVE | LK_RETRY, td);
2450 	if (VOP_GETVOBJECT(vp, &obj) == 0)
2451 		vm_object_page_clean(obj, 0, 0, 0);
2452 	if ((error = VOP_FSYNC(vp, MNT_WAIT, td)) == 0 &&
2453 	    vp->v_mount && (vp->v_mount->mnt_flag & MNT_SOFTDEP) &&
2454 	    bioops.io_fsync)
2455 		error = (*bioops.io_fsync)(vp);
2456 	VOP_UNLOCK(vp, 0, td);
2457 	return (error);
2458 }
2459 
2460 int
2461 kern_rename(struct nlookupdata *fromnd, struct nlookupdata *tond)
2462 {
2463 	struct namecache *fncpd;
2464 	struct namecache *tncpd;
2465 	struct namecache *ncp;
2466 	struct mount *mp;
2467 	int error;
2468 
2469 	bwillwrite();
2470 	if ((error = nlookup(fromnd)) != 0)
2471 		return (error);
2472 	if ((fncpd = fromnd->nl_ncp->nc_parent) == NULL)
2473 		return (ENOENT);
2474 	cache_hold(fncpd);
2475 
2476 	/*
2477 	 * unlock the source ncp so we can lookup the target ncp without
2478 	 * deadlocking.  The target may or may not exist so we do not check
2479 	 * for a target vp like kern_mkdir() and other creation functions do.
2480 	 *
2481 	 * The source and target directories are ref'd and rechecked after
2482 	 * everything is relocked to determine if the source or target file
2483 	 * has been renamed.
2484 	 */
2485 	KKASSERT(fromnd->nl_flags & NLC_NCPISLOCKED);
2486 	fromnd->nl_flags &= ~NLC_NCPISLOCKED;
2487 	cache_unlock(fromnd->nl_ncp);
2488 
2489 	tond->nl_flags |= NLC_CREATE;
2490 	if ((error = nlookup(tond)) != 0) {
2491 		cache_drop(fncpd);
2492 		return (error);
2493 	}
2494 	if ((tncpd = tond->nl_ncp->nc_parent) == NULL) {
2495 		cache_drop(fncpd);
2496 		return (ENOENT);
2497 	}
2498 	cache_hold(tncpd);
2499 
2500 	/*
2501 	 * If the source and target are the same there is nothing to do
2502 	 */
2503 	if (fromnd->nl_ncp == tond->nl_ncp) {
2504 		cache_drop(fncpd);
2505 		cache_drop(tncpd);
2506 		return (0);
2507 	}
2508 
2509 	/*
2510 	 * relock the source ncp
2511 	 */
2512 	if (cache_lock_nonblock(fromnd->nl_ncp) == 0) {
2513 		cache_resolve(fromnd->nl_ncp, fromnd->nl_cred);
2514 	} else if (fromnd->nl_ncp > tond->nl_ncp) {
2515 		cache_lock(fromnd->nl_ncp);
2516 		cache_resolve(fromnd->nl_ncp, fromnd->nl_cred);
2517 	} else {
2518 		cache_unlock(tond->nl_ncp);
2519 		cache_lock(fromnd->nl_ncp);
2520 		cache_resolve(fromnd->nl_ncp, fromnd->nl_cred);
2521 		cache_lock(tond->nl_ncp);
2522 		cache_resolve(tond->nl_ncp, tond->nl_cred);
2523 	}
2524 	fromnd->nl_flags |= NLC_NCPISLOCKED;
2525 
2526 	/*
2527 	 * make sure the parent directories linkages are the same
2528 	 */
2529 	if (fncpd != fromnd->nl_ncp->nc_parent ||
2530 	    tncpd != tond->nl_ncp->nc_parent) {
2531 		cache_drop(fncpd);
2532 		cache_drop(tncpd);
2533 		return (ENOENT);
2534 	}
2535 
2536 	/*
2537 	 * Both the source and target must be within the same filesystem and
2538 	 * in the same filesystem as their parent directories within the
2539 	 * namecache topology.
2540 	 */
2541 	mp = fncpd->nc_mount;
2542 	if (mp != tncpd->nc_mount || mp != fromnd->nl_ncp->nc_mount ||
2543 	    mp != tond->nl_ncp->nc_mount) {
2544 		cache_drop(fncpd);
2545 		cache_drop(tncpd);
2546 		return (EXDEV);
2547 	}
2548 
2549 	/*
2550 	 * If the target exists and either the source or target is a directory,
2551 	 * then both must be directories.
2552 	 */
2553 	if (tond->nl_ncp->nc_vp) {
2554 		if (fromnd->nl_ncp->nc_vp->v_type == VDIR) {
2555 			if (tond->nl_ncp->nc_vp->v_type != VDIR)
2556 				error = ENOTDIR;
2557 		} else if (tond->nl_ncp->nc_vp->v_type == VDIR) {
2558 			error = EISDIR;
2559 		}
2560 	}
2561 
2562 	/*
2563 	 * You cannot rename a source into itself or a subdirectory of itself.
2564 	 * We check this by travsersing the target directory upwards looking
2565 	 * for a match against the source.
2566 	 */
2567 	if (error == 0) {
2568 		for (ncp = tncpd; ncp; ncp = ncp->nc_parent) {
2569 			if (fromnd->nl_ncp == ncp) {
2570 				error = EINVAL;
2571 				break;
2572 			}
2573 		}
2574 	}
2575 
2576 	cache_drop(fncpd);
2577 	cache_drop(tncpd);
2578 	if (error)
2579 		return (error);
2580 	error = VOP_NRENAME(fromnd->nl_ncp, tond->nl_ncp, tond->nl_cred);
2581 	return (error);
2582 }
2583 
2584 /*
2585  * rename_args(char *from, char *to)
2586  *
2587  * Rename files.  Source and destination must either both be directories,
2588  * or both not be directories.  If target is a directory, it must be empty.
2589  */
2590 int
2591 rename(struct rename_args *uap)
2592 {
2593 	struct nlookupdata fromnd, tond;
2594 	int error;
2595 
2596 	error = nlookup_init(&fromnd, uap->from, UIO_USERSPACE, 0);
2597 	if (error == 0) {
2598 		error = nlookup_init(&tond, uap->to, UIO_USERSPACE, 0);
2599 		if (error == 0)
2600 			error = kern_rename(&fromnd, &tond);
2601 		nlookup_done(&tond);
2602 	}
2603 	nlookup_done(&fromnd);
2604 	return (error);
2605 }
2606 
2607 int
2608 kern_mkdir(struct nlookupdata *nd, int mode)
2609 {
2610 	struct thread *td = curthread;
2611 	struct proc *p = td->td_proc;
2612 	struct namecache *ncp;
2613 	struct vnode *vp;
2614 	struct vattr vattr;
2615 	int error;
2616 
2617 	bwillwrite();
2618 	nd->nl_flags |= NLC_WILLBEDIR | NLC_CREATE;
2619 	if ((error = nlookup(nd)) != 0)
2620 		return (error);
2621 
2622 	ncp = nd->nl_ncp;
2623 	if (ncp->nc_vp)
2624 		return (EEXIST);
2625 
2626 	VATTR_NULL(&vattr);
2627 	vattr.va_type = VDIR;
2628 	vattr.va_mode = (mode & ACCESSPERMS) &~ p->p_fd->fd_cmask;
2629 
2630 	vp = NULL;
2631 	error = VOP_NMKDIR(ncp, &vp, p->p_ucred, &vattr);
2632 	if (error == 0)
2633 		vput(vp);
2634 	return (error);
2635 }
2636 
2637 /*
2638  * mkdir_args(char *path, int mode)
2639  *
2640  * Make a directory file.
2641  */
2642 /* ARGSUSED */
2643 int
2644 mkdir(struct mkdir_args *uap)
2645 {
2646 	struct nlookupdata nd;
2647 	int error;
2648 
2649 	error = nlookup_init(&nd, uap->path, UIO_USERSPACE, 0);
2650 	if (error == 0)
2651 		error = kern_mkdir(&nd, uap->mode);
2652 	nlookup_done(&nd);
2653 	return (error);
2654 }
2655 
2656 int
2657 kern_rmdir(struct nlookupdata *nd)
2658 {
2659 	struct namecache *ncp;
2660 	int error;
2661 
2662 	bwillwrite();
2663 	nd->nl_flags |= NLC_DELETE;
2664 	if ((error = nlookup(nd)) != 0)
2665 		return (error);
2666 
2667 	ncp = nd->nl_ncp;
2668 	error = VOP_NRMDIR(ncp, nd->nl_cred);
2669 	return (error);
2670 }
2671 
2672 /*
2673  * rmdir_args(char *path)
2674  *
2675  * Remove a directory file.
2676  */
2677 /* ARGSUSED */
2678 int
2679 rmdir(struct rmdir_args *uap)
2680 {
2681 	struct nlookupdata nd;
2682 	int error;
2683 
2684 	error = nlookup_init(&nd, uap->path, UIO_USERSPACE, 0);
2685 	if (error == 0)
2686 		error = kern_rmdir(&nd);
2687 	nlookup_done(&nd);
2688 	return (error);
2689 }
2690 
2691 int
2692 kern_getdirentries(int fd, char *buf, u_int count, long *basep, int *res)
2693 {
2694 	struct thread *td = curthread;
2695 	struct proc *p = td->td_proc;
2696 	struct vnode *vp;
2697 	struct file *fp;
2698 	struct uio auio;
2699 	struct iovec aiov;
2700 	long loff;
2701 	int error, eofflag;
2702 
2703 	if ((error = getvnode(p->p_fd, fd, &fp)) != 0)
2704 		return (error);
2705 	if ((fp->f_flag & FREAD) == 0)
2706 		return (EBADF);
2707 	vp = (struct vnode *)fp->f_data;
2708 unionread:
2709 	if (vp->v_type != VDIR)
2710 		return (EINVAL);
2711 	aiov.iov_base = buf;
2712 	aiov.iov_len = count;
2713 	auio.uio_iov = &aiov;
2714 	auio.uio_iovcnt = 1;
2715 	auio.uio_rw = UIO_READ;
2716 	auio.uio_segflg = UIO_USERSPACE;
2717 	auio.uio_td = td;
2718 	auio.uio_resid = count;
2719 	/* vn_lock(vp, LK_SHARED | LK_RETRY, td); */
2720 	vn_lock(vp, LK_EXCLUSIVE | LK_RETRY, td);
2721 	loff = auio.uio_offset = fp->f_offset;
2722 	error = VOP_READDIR(vp, &auio, fp->f_cred, &eofflag, NULL, NULL);
2723 	fp->f_offset = auio.uio_offset;
2724 	VOP_UNLOCK(vp, 0, td);
2725 	if (error)
2726 		return (error);
2727 	if (count == auio.uio_resid) {
2728 		if (union_dircheckp) {
2729 			error = union_dircheckp(td, &vp, fp);
2730 			if (error == -1)
2731 				goto unionread;
2732 			if (error)
2733 				return (error);
2734 		}
2735 		if ((vp->v_flag & VROOT) &&
2736 		    (vp->v_mount->mnt_flag & MNT_UNION)) {
2737 			struct vnode *tvp = vp;
2738 			vp = vp->v_mount->mnt_vnodecovered;
2739 			vref(vp);
2740 			fp->f_data = (caddr_t)vp;
2741 			fp->f_offset = 0;
2742 			vrele(tvp);
2743 			goto unionread;
2744 		}
2745 	}
2746 	if (basep) {
2747 		*basep = loff;
2748 	}
2749 	*res = count - auio.uio_resid;
2750 	return (error);
2751 }
2752 
2753 /*
2754  * getdirentries_args(int fd, char *buf, u_int conut, long *basep)
2755  *
2756  * Read a block of directory entries in a file system independent format.
2757  */
2758 int
2759 getdirentries(struct getdirentries_args *uap)
2760 {
2761 	long base;
2762 	int error;
2763 
2764 	error = kern_getdirentries(uap->fd, uap->buf, uap->count, &base,
2765 	    &uap->sysmsg_result);
2766 
2767 	if (error == 0)
2768 		error = copyout(&base, uap->basep, sizeof(*uap->basep));
2769 	return (error);
2770 }
2771 
2772 /*
2773  * getdents_args(int fd, char *buf, size_t count)
2774  */
2775 int
2776 getdents(struct getdents_args *uap)
2777 {
2778 	int error;
2779 
2780 	error = kern_getdirentries(uap->fd, uap->buf, uap->count, NULL,
2781 	    &uap->sysmsg_result);
2782 
2783 	return (error);
2784 }
2785 
2786 /*
2787  * umask(int newmask)
2788  *
2789  * Set the mode mask for creation of filesystem nodes.
2790  *
2791  * MP SAFE
2792  */
2793 int
2794 umask(struct umask_args *uap)
2795 {
2796 	struct thread *td = curthread;
2797 	struct proc *p = td->td_proc;
2798 	struct filedesc *fdp;
2799 
2800 	fdp = p->p_fd;
2801 	uap->sysmsg_result = fdp->fd_cmask;
2802 	fdp->fd_cmask = SCARG(uap, newmask) & ALLPERMS;
2803 	return (0);
2804 }
2805 
2806 /*
2807  * revoke(char *path)
2808  *
2809  * Void all references to file by ripping underlying filesystem
2810  * away from vnode.
2811  */
2812 /* ARGSUSED */
2813 int
2814 revoke(struct revoke_args *uap)
2815 {
2816 	struct thread *td = curthread;
2817 	struct nlookupdata nd;
2818 	struct vattr vattr;
2819 	struct vnode *vp;
2820 	struct ucred *cred;
2821 	int error;
2822 
2823 	vp = NULL;
2824 	error = nlookup_init(&nd, SCARG(uap, path), UIO_USERSPACE, NLC_FOLLOW);
2825 	if (error == 0)
2826 		error = nlookup(&nd);
2827 	if (error == 0)
2828 		error = cache_vref(nd.nl_ncp, nd.nl_cred, &vp);
2829 	cred = crhold(nd.nl_cred);
2830 	nlookup_done(&nd);
2831 	if (error == 0) {
2832 		if (vp->v_type != VCHR && vp->v_type != VBLK)
2833 			error = EINVAL;
2834 		if (error == 0)
2835 			error = VOP_GETATTR(vp, &vattr, td);
2836 		if (error == 0 && cred->cr_uid != vattr.va_uid)
2837 			error = suser_cred(cred, PRISON_ROOT);
2838 		if (error == 0 && count_udev(vp->v_udev) > 0) {
2839 			if ((error = vx_lock(vp)) == 0) {
2840 				VOP_REVOKE(vp, REVOKEALL);
2841 				vx_unlock(vp);
2842 			}
2843 		}
2844 		vrele(vp);
2845 	}
2846 	crfree(cred);
2847 	return (error);
2848 }
2849 
2850 /*
2851  * Convert a user file descriptor to a kernel file entry.
2852  */
2853 int
2854 getvnode(struct filedesc *fdp, int fd, struct file **fpp)
2855 {
2856 	struct file *fp;
2857 
2858 	if ((u_int)fd >= fdp->fd_nfiles ||
2859 	    (fp = fdp->fd_ofiles[fd]) == NULL)
2860 		return (EBADF);
2861 	if (fp->f_type != DTYPE_VNODE && fp->f_type != DTYPE_FIFO)
2862 		return (EINVAL);
2863 	*fpp = fp;
2864 	return (0);
2865 }
2866 /*
2867  * getfh_args(char *fname, fhandle_t *fhp)
2868  *
2869  * Get (NFS) file handle
2870  */
2871 int
2872 getfh(struct getfh_args *uap)
2873 {
2874 	struct thread *td = curthread;
2875 	struct nlookupdata nd;
2876 	fhandle_t fh;
2877 	struct vnode *vp;
2878 	int error;
2879 
2880 	/*
2881 	 * Must be super user
2882 	 */
2883 	if ((error = suser(td)) != 0)
2884 		return (error);
2885 
2886 	vp = NULL;
2887 	error = nlookup_init(&nd, uap->fname, UIO_USERSPACE, NLC_FOLLOW);
2888 	if (error == 0)
2889 		error = nlookup(&nd);
2890 	if (error == 0)
2891 		error = cache_vget(nd.nl_ncp, nd.nl_cred, LK_EXCLUSIVE, &vp);
2892 	nlookup_done(&nd);
2893 	if (error == 0) {
2894 		bzero(&fh, sizeof(fh));
2895 		fh.fh_fsid = vp->v_mount->mnt_stat.f_fsid;
2896 		error = VFS_VPTOFH(vp, &fh.fh_fid);
2897 		vput(vp);
2898 		if (error == 0)
2899 			error = copyout(&fh, uap->fhp, sizeof(fh));
2900 	}
2901 	return (error);
2902 }
2903 
2904 /*
2905  * fhopen_args(const struct fhandle *u_fhp, int flags)
2906  *
2907  * syscall for the rpc.lockd to use to translate a NFS file handle into
2908  * an open descriptor.
2909  *
2910  * warning: do not remove the suser() call or this becomes one giant
2911  * security hole.
2912  */
2913 int
2914 fhopen(struct fhopen_args *uap)
2915 {
2916 	struct thread *td = curthread;
2917 	struct proc *p = td->td_proc;
2918 	struct mount *mp;
2919 	struct vnode *vp;
2920 	struct fhandle fhp;
2921 	struct vattr vat;
2922 	struct vattr *vap = &vat;
2923 	struct flock lf;
2924 	struct filedesc *fdp = p->p_fd;
2925 	int fmode, mode, error, type;
2926 	struct file *nfp;
2927 	struct file *fp;
2928 	int indx;
2929 
2930 	/*
2931 	 * Must be super user
2932 	 */
2933 	error = suser(td);
2934 	if (error)
2935 		return (error);
2936 
2937 	fmode = FFLAGS(SCARG(uap, flags));
2938 	/* why not allow a non-read/write open for our lockd? */
2939 	if (((fmode & (FREAD | FWRITE)) == 0) || (fmode & O_CREAT))
2940 		return (EINVAL);
2941 	error = copyin(SCARG(uap,u_fhp), &fhp, sizeof(fhp));
2942 	if (error)
2943 		return(error);
2944 	/* find the mount point */
2945 	mp = vfs_getvfs(&fhp.fh_fsid);
2946 	if (mp == NULL)
2947 		return (ESTALE);
2948 	/* now give me my vnode, it gets returned to me locked */
2949 	error = VFS_FHTOVP(mp, &fhp.fh_fid, &vp);
2950 	if (error)
2951 		return (error);
2952  	/*
2953 	 * from now on we have to make sure not
2954 	 * to forget about the vnode
2955 	 * any error that causes an abort must vput(vp)
2956 	 * just set error = err and 'goto bad;'.
2957 	 */
2958 
2959 	/*
2960 	 * from vn_open
2961 	 */
2962 	if (vp->v_type == VLNK) {
2963 		error = EMLINK;
2964 		goto bad;
2965 	}
2966 	if (vp->v_type == VSOCK) {
2967 		error = EOPNOTSUPP;
2968 		goto bad;
2969 	}
2970 	mode = 0;
2971 	if (fmode & (FWRITE | O_TRUNC)) {
2972 		if (vp->v_type == VDIR) {
2973 			error = EISDIR;
2974 			goto bad;
2975 		}
2976 		error = vn_writechk(vp);
2977 		if (error)
2978 			goto bad;
2979 		mode |= VWRITE;
2980 	}
2981 	if (fmode & FREAD)
2982 		mode |= VREAD;
2983 	if (mode) {
2984 		error = VOP_ACCESS(vp, mode, p->p_ucred, td);
2985 		if (error)
2986 			goto bad;
2987 	}
2988 	if (fmode & O_TRUNC) {
2989 		VOP_UNLOCK(vp, 0, td);			/* XXX */
2990 		VOP_LEASE(vp, td, p->p_ucred, LEASE_WRITE);
2991 		vn_lock(vp, LK_EXCLUSIVE | LK_RETRY, td);	/* XXX */
2992 		VATTR_NULL(vap);
2993 		vap->va_size = 0;
2994 		error = VOP_SETATTR(vp, vap, p->p_ucred, td);
2995 		if (error)
2996 			goto bad;
2997 	}
2998 
2999 	/*
3000 	 * VOP_OPEN needs the file pointer so it can potentially override
3001 	 * it.
3002 	 *
3003 	 * WARNING! no f_ncp will be associated when fhopen()ing a directory.
3004 	 * XXX
3005 	 */
3006 	if ((error = falloc(p, &nfp, NULL)) != 0)
3007 		goto bad;
3008 	fp = nfp;
3009 
3010 	fp->f_data = (caddr_t)vp;
3011 	fp->f_flag = fmode & FMASK;
3012 	fp->f_ops = &vnode_fileops;
3013 	fp->f_type = DTYPE_VNODE;
3014 
3015 	error = VOP_OPEN(vp, fmode, p->p_ucred, fp, td);
3016 	if (error) {
3017 		/*
3018 		 * setting f_ops this way prevents VOP_CLOSE from being
3019 		 * called or fdrop() releasing the vp from v_data.   Since
3020 		 * the VOP_OPEN failed we don't want to VOP_CLOSE.
3021 		 */
3022 		fp->f_ops = &badfileops;
3023 		fp->f_data = NULL;
3024 		fdrop(fp, td);
3025 		goto bad;
3026 	}
3027 	if (fmode & FWRITE)
3028 		vp->v_writecount++;
3029 
3030 	/*
3031 	 * The fp now owns a reference on the vnode.  We still have our own
3032 	 * ref+lock.
3033 	 */
3034 	vref(vp);
3035 
3036 	/*
3037 	 * Make sure that a VM object is created for VMIO support.  If this
3038 	 * fails just fdrop() normally to clean up.
3039 	 */
3040 	if (vn_canvmio(vp) == TRUE) {
3041 		if ((error = vfs_object_create(vp, td)) != 0) {
3042 			fdrop(fp, td);
3043 			goto bad;
3044 		}
3045 	}
3046 
3047 	/*
3048 	 * The open was successful, associate it with a file descriptor.
3049 	 */
3050 	if ((error = fsetfd(p, fp, &indx)) != 0) {
3051 		if (fmode & FWRITE)
3052 			vp->v_writecount--;
3053 		fdrop(fp, td);
3054 		goto bad;
3055 	}
3056 
3057 	if (fmode & (O_EXLOCK | O_SHLOCK)) {
3058 		lf.l_whence = SEEK_SET;
3059 		lf.l_start = 0;
3060 		lf.l_len = 0;
3061 		if (fmode & O_EXLOCK)
3062 			lf.l_type = F_WRLCK;
3063 		else
3064 			lf.l_type = F_RDLCK;
3065 		type = F_FLOCK;
3066 		if ((fmode & FNONBLOCK) == 0)
3067 			type |= F_WAIT;
3068 		VOP_UNLOCK(vp, 0, td);
3069 		if ((error = VOP_ADVLOCK(vp, (caddr_t)fp, F_SETLK, &lf, type)) != 0) {
3070 			/*
3071 			 * lock request failed.  Normally close the descriptor
3072 			 * but handle the case where someone might have dup()d
3073 			 * or close()d it when we weren't looking.
3074 			 */
3075 			if (fdp->fd_ofiles[indx] == fp) {
3076 				fdp->fd_ofiles[indx] = NULL;
3077 				fdrop(fp, td);
3078 			}
3079 
3080 			/*
3081 			 * release our private reference.
3082 			 */
3083 			fdrop(fp, td);
3084 			vrele(vp);
3085 			return (error);
3086 		}
3087 		vn_lock(vp, LK_EXCLUSIVE | LK_RETRY, td);
3088 		fp->f_flag |= FHASLOCK;
3089 	}
3090 	if ((vp->v_type == VREG) && (VOP_GETVOBJECT(vp, NULL) != 0))
3091 		vfs_object_create(vp, td);
3092 
3093 	vput(vp);
3094 	fdrop(fp, td);
3095 	uap->sysmsg_result = indx;
3096 	return (0);
3097 
3098 bad:
3099 	vput(vp);
3100 	return (error);
3101 }
3102 
3103 /*
3104  * fhstat_args(struct fhandle *u_fhp, struct stat *sb)
3105  */
3106 int
3107 fhstat(struct fhstat_args *uap)
3108 {
3109 	struct thread *td = curthread;
3110 	struct stat sb;
3111 	fhandle_t fh;
3112 	struct mount *mp;
3113 	struct vnode *vp;
3114 	int error;
3115 
3116 	/*
3117 	 * Must be super user
3118 	 */
3119 	error = suser(td);
3120 	if (error)
3121 		return (error);
3122 
3123 	error = copyin(SCARG(uap, u_fhp), &fh, sizeof(fhandle_t));
3124 	if (error)
3125 		return (error);
3126 
3127 	if ((mp = vfs_getvfs(&fh.fh_fsid)) == NULL)
3128 		return (ESTALE);
3129 	if ((error = VFS_FHTOVP(mp, &fh.fh_fid, &vp)))
3130 		return (error);
3131 	error = vn_stat(vp, &sb, td);
3132 	vput(vp);
3133 	if (error)
3134 		return (error);
3135 	error = copyout(&sb, SCARG(uap, sb), sizeof(sb));
3136 	return (error);
3137 }
3138 
3139 /*
3140  * fhstatfs_args(struct fhandle *u_fhp, struct statfs *buf)
3141  */
3142 int
3143 fhstatfs(struct fhstatfs_args *uap)
3144 {
3145 	struct thread *td = curthread;
3146 	struct statfs *sp;
3147 	struct mount *mp;
3148 	struct vnode *vp;
3149 	struct statfs sb;
3150 	fhandle_t fh;
3151 	int error;
3152 
3153 	/*
3154 	 * Must be super user
3155 	 */
3156 	if ((error = suser(td)))
3157 		return (error);
3158 
3159 	if ((error = copyin(SCARG(uap, u_fhp), &fh, sizeof(fhandle_t))) != 0)
3160 		return (error);
3161 
3162 	if ((mp = vfs_getvfs(&fh.fh_fsid)) == NULL)
3163 		return (ESTALE);
3164 	if ((error = VFS_FHTOVP(mp, &fh.fh_fid, &vp)))
3165 		return (error);
3166 	mp = vp->v_mount;
3167 	sp = &mp->mnt_stat;
3168 	vput(vp);
3169 	if ((error = VFS_STATFS(mp, sp, td)) != 0)
3170 		return (error);
3171 	sp->f_flags = mp->mnt_flag & MNT_VISFLAGMASK;
3172 	if (suser(td)) {
3173 		bcopy(sp, &sb, sizeof(sb));
3174 		sb.f_fsid.val[0] = sb.f_fsid.val[1] = 0;
3175 		sp = &sb;
3176 	}
3177 	return (copyout(sp, SCARG(uap, buf), sizeof(*sp)));
3178 }
3179 
3180 /*
3181  * Syscall to push extended attribute configuration information into the
3182  * VFS.  Accepts a path, which it converts to a mountpoint, as well as
3183  * a command (int cmd), and attribute name and misc data.  For now, the
3184  * attribute name is left in userspace for consumption by the VFS_op.
3185  * It will probably be changed to be copied into sysspace by the
3186  * syscall in the future, once issues with various consumers of the
3187  * attribute code have raised their hands.
3188  *
3189  * Currently this is used only by UFS Extended Attributes.
3190  */
3191 int
3192 extattrctl(struct extattrctl_args *uap)
3193 {
3194 	struct nlookupdata nd;
3195 	struct mount *mp;
3196 	struct vnode *vp;
3197 	int error;
3198 
3199 	vp = NULL;
3200 	error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
3201 	if (error == 0)
3202 		error = nlookup(&nd);
3203 	if (error == 0) {
3204 		mp = nd.nl_ncp->nc_mount;
3205 		error = VFS_EXTATTRCTL(mp, SCARG(uap, cmd),
3206 				SCARG(uap, attrname), SCARG(uap, arg),
3207 				nd.nl_td);
3208 	}
3209 	nlookup_done(&nd);
3210 	return (error);
3211 }
3212 
3213 /*
3214  * Syscall to set a named extended attribute on a file or directory.
3215  * Accepts attribute name, and a uio structure pointing to the data to set.
3216  * The uio is consumed in the style of writev().  The real work happens
3217  * in VOP_SETEXTATTR().
3218  */
3219 int
3220 extattr_set_file(struct extattr_set_file_args *uap)
3221 {
3222 	char attrname[EXTATTR_MAXNAMELEN];
3223 	struct iovec aiov[UIO_SMALLIOV];
3224 	struct iovec *needfree;
3225 	struct nlookupdata nd;
3226 	struct iovec *iov;
3227 	struct vnode *vp;
3228 	struct uio auio;
3229 	u_int iovlen;
3230 	u_int cnt;
3231 	int error;
3232 	int i;
3233 
3234 	error = copyin(SCARG(uap, attrname), attrname, EXTATTR_MAXNAMELEN);
3235 	if (error)
3236 		return (error);
3237 
3238 	vp = NULL;
3239 	error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
3240 	if (error == 0)
3241 		error = nlookup(&nd);
3242 	if (error == 0)
3243 		error = cache_vget(nd.nl_ncp, nd.nl_cred, LK_EXCLUSIVE, &vp);
3244 	if (error) {
3245 		nlookup_done(&nd);
3246 		return (error);
3247 	}
3248 
3249 	needfree = NULL;
3250 	iovlen = uap->iovcnt * sizeof(struct iovec);
3251 	if (uap->iovcnt > UIO_SMALLIOV) {
3252 		if (uap->iovcnt > UIO_MAXIOV) {
3253 			error = EINVAL;
3254 			goto done;
3255 		}
3256 		MALLOC(iov, struct iovec *, iovlen, M_IOV, M_WAITOK);
3257 		needfree = iov;
3258 	} else {
3259 		iov = aiov;
3260 	}
3261 	auio.uio_iov = iov;
3262 	auio.uio_iovcnt = uap->iovcnt;
3263 	auio.uio_rw = UIO_WRITE;
3264 	auio.uio_segflg = UIO_USERSPACE;
3265 	auio.uio_td = nd.nl_td;
3266 	auio.uio_offset = 0;
3267 	if ((error = copyin(uap->iovp, iov, iovlen)))
3268 		goto done;
3269 	auio.uio_resid = 0;
3270 	for (i = 0; i < uap->iovcnt; i++) {
3271 		if (iov->iov_len > INT_MAX - auio.uio_resid) {
3272 			error = EINVAL;
3273 			goto done;
3274 		}
3275 		auio.uio_resid += iov->iov_len;
3276 		iov++;
3277 	}
3278 	cnt = auio.uio_resid;
3279 	error = VOP_SETEXTATTR(vp, attrname, &auio, nd.nl_cred, nd.nl_td);
3280 	cnt -= auio.uio_resid;
3281 	uap->sysmsg_result = cnt;
3282 done:
3283 	vput(vp);
3284 	nlookup_done(&nd);
3285 	if (needfree)
3286 		FREE(needfree, M_IOV);
3287 	return (error);
3288 }
3289 
3290 /*
3291  * Syscall to get a named extended attribute on a file or directory.
3292  * Accepts attribute name, and a uio structure pointing to a buffer for the
3293  * data.  The uio is consumed in the style of readv().  The real work
3294  * happens in VOP_GETEXTATTR();
3295  */
3296 int
3297 extattr_get_file(struct extattr_get_file_args *uap)
3298 {
3299 	char attrname[EXTATTR_MAXNAMELEN];
3300 	struct iovec aiov[UIO_SMALLIOV];
3301 	struct iovec *needfree;
3302 	struct nlookupdata nd;
3303 	struct iovec *iov;
3304 	struct vnode *vp;
3305 	struct uio auio;
3306 	u_int iovlen;
3307 	u_int cnt;
3308 	int error;
3309 	int i;
3310 
3311 	error = copyin(SCARG(uap, attrname), attrname, EXTATTR_MAXNAMELEN);
3312 	if (error)
3313 		return (error);
3314 
3315 	vp = NULL;
3316 	error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
3317 	if (error == 0)
3318 		error = nlookup(&nd);
3319 	if (error == 0)
3320 		error = cache_vget(nd.nl_ncp, nd.nl_cred, LK_EXCLUSIVE, &vp);
3321 	if (error) {
3322 		nlookup_done(&nd);
3323 		return (error);
3324 	}
3325 
3326 	iovlen = uap->iovcnt * sizeof (struct iovec);
3327 	needfree = NULL;
3328 	if (uap->iovcnt > UIO_SMALLIOV) {
3329 		if (uap->iovcnt > UIO_MAXIOV) {
3330 			error = EINVAL;
3331 			goto done;
3332 		}
3333 		MALLOC(iov, struct iovec *, iovlen, M_IOV, M_WAITOK);
3334 		needfree = iov;
3335 	} else {
3336 		iov = aiov;
3337 	}
3338 	auio.uio_iov = iov;
3339 	auio.uio_iovcnt = uap->iovcnt;
3340 	auio.uio_rw = UIO_READ;
3341 	auio.uio_segflg = UIO_USERSPACE;
3342 	auio.uio_td = nd.nl_td;
3343 	auio.uio_offset = 0;
3344 	if ((error = copyin(uap->iovp, iov, iovlen)))
3345 		goto done;
3346 	auio.uio_resid = 0;
3347 	for (i = 0; i < uap->iovcnt; i++) {
3348 		if (iov->iov_len > INT_MAX - auio.uio_resid) {
3349 			error = EINVAL;
3350 			goto done;
3351 		}
3352 		auio.uio_resid += iov->iov_len;
3353 		iov++;
3354 	}
3355 	cnt = auio.uio_resid;
3356 	error = VOP_GETEXTATTR(vp, attrname, &auio, nd.nl_cred, nd.nl_td);
3357 	cnt -= auio.uio_resid;
3358 	uap->sysmsg_result = cnt;
3359 done:
3360 	vput(vp);
3361 	nlookup_done(&nd);
3362 	if (needfree)
3363 		FREE(needfree, M_IOV);
3364 	return(error);
3365 }
3366 
3367 /*
3368  * Syscall to delete a named extended attribute from a file or directory.
3369  * Accepts attribute name.  The real work happens in VOP_SETEXTATTR().
3370  */
3371 int
3372 extattr_delete_file(struct extattr_delete_file_args *uap)
3373 {
3374 	char attrname[EXTATTR_MAXNAMELEN];
3375 	struct nlookupdata nd;
3376 	struct vnode *vp;
3377 	int error;
3378 
3379 	error = copyin(SCARG(uap, attrname), attrname, EXTATTR_MAXNAMELEN);
3380 	if (error)
3381 		return(error);
3382 
3383 	vp = NULL;
3384 	error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
3385 	if (error == 0)
3386 		error = nlookup(&nd);
3387 	if (error == 0)
3388 		error = cache_vget(nd.nl_ncp, nd.nl_cred, LK_EXCLUSIVE, &vp);
3389 	if (error) {
3390 		nlookup_done(&nd);
3391 		return (error);
3392 	}
3393 
3394 	error = VOP_SETEXTATTR(vp, attrname, NULL, nd.nl_cred, nd.nl_td);
3395 	vput(vp);
3396 	nlookup_done(&nd);
3397 	return(error);
3398 }
3399 
3400 /*
3401  * print out statistics from the current status of the buffer pool
3402  * this can be toggeled by the system control option debug.syncprt
3403  */
3404 #ifdef DEBUG
3405 void
3406 vfs_bufstats(void)
3407 {
3408         int s, i, j, count;
3409         struct buf *bp;
3410         struct bqueues *dp;
3411         int counts[(MAXBSIZE / PAGE_SIZE) + 1];
3412         static char *bname[3] = { "LOCKED", "LRU", "AGE" };
3413 
3414         for (dp = bufqueues, i = 0; dp < &bufqueues[3]; dp++, i++) {
3415                 count = 0;
3416                 for (j = 0; j <= MAXBSIZE/PAGE_SIZE; j++)
3417                         counts[j] = 0;
3418                 s = splbio();
3419                 TAILQ_FOREACH(bp, dp, b_freelist) {
3420                         counts[bp->b_bufsize/PAGE_SIZE]++;
3421                         count++;
3422                 }
3423                 splx(s);
3424                 printf("%s: total-%d", bname[i], count);
3425                 for (j = 0; j <= MAXBSIZE/PAGE_SIZE; j++)
3426                         if (counts[j] != 0)
3427                                 printf(", %d-%d", j * PAGE_SIZE, counts[j]);
3428                 printf("\n");
3429         }
3430 }
3431 #endif
3432