xref: /dragonfly/sys/kern/vfs_syscalls.c (revision 655933d6)
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. Neither the name of the University nor the names of its contributors
19  *    may be used to endorse or promote products derived from this software
20  *    without specific prior written permission.
21  *
22  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
23  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
26  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
27  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
28  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
29  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
31  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32  * SUCH DAMAGE.
33  *
34  *	@(#)vfs_syscalls.c	8.13 (Berkeley) 4/15/94
35  * $FreeBSD: src/sys/kern/vfs_syscalls.c,v 1.151.2.18 2003/04/04 20:35:58 tegge Exp $
36  */
37 
38 #include <sys/param.h>
39 #include <sys/systm.h>
40 #include <sys/buf.h>
41 #include <sys/conf.h>
42 #include <sys/sysent.h>
43 #include <sys/malloc.h>
44 #include <sys/mount.h>
45 #include <sys/mountctl.h>
46 #include <sys/sysmsg.h>
47 #include <sys/filedesc.h>
48 #include <sys/kernel.h>
49 #include <sys/fcntl.h>
50 #include <sys/file.h>
51 #include <sys/linker.h>
52 #include <sys/stat.h>
53 #include <sys/unistd.h>
54 #include <sys/vnode.h>
55 #include <sys/proc.h>
56 #include <sys/priv.h>
57 #include <sys/jail.h>
58 #include <sys/namei.h>
59 #include <sys/nlookup.h>
60 #include <sys/dirent.h>
61 #include <sys/extattr.h>
62 #include <sys/spinlock.h>
63 #include <sys/kern_syscall.h>
64 #include <sys/objcache.h>
65 #include <sys/sysctl.h>
66 
67 #include <sys/buf2.h>
68 #include <sys/file2.h>
69 #include <sys/spinlock2.h>
70 
71 #include <vm/vm.h>
72 #include <vm/vm_object.h>
73 #include <vm/vm_page.h>
74 
75 #include <machine/limits.h>
76 #include <machine/stdarg.h>
77 
78 static void mount_warning(struct mount *mp, const char *ctl, ...)
79 		__printflike(2, 3);
80 static int mount_path(struct proc *p, struct mount *mp, char **rb, char **fb);
81 static int checkvp_chdir (struct vnode *vn, struct thread *td);
82 static void checkdirs (struct nchandle *old_nch, struct nchandle *new_nch);
83 static int get_fspriv(const char *);
84 static int chroot_refuse_vdir_fds (thread_t td, struct filedesc *fdp);
85 static int chroot_visible_mnt(struct mount *mp, struct proc *p);
86 static int getutimes (struct timeval *, struct timespec *);
87 static int getutimens (const struct timespec *, struct timespec *, int *);
88 static int setfown (struct mount *, struct vnode *, uid_t, gid_t);
89 static int setfmode (struct vnode *, int);
90 static int setfflags (struct vnode *, u_long);
91 static int setutimes (struct vnode *, struct vattr *,
92 			const struct timespec *, int);
93 
94 static int	usermount = 0;	/* if 1, non-root can mount fs. */
95 SYSCTL_INT(_vfs, OID_AUTO, usermount, CTLFLAG_RW, &usermount, 0,
96     "Allow non-root users to mount filesystems");
97 
98 static int	debug_unmount = 0; /* if 1 loop until unmount success */
99 SYSCTL_INT(_vfs, OID_AUTO, debug_unmount, CTLFLAG_RW, &debug_unmount, 0,
100     "Stall failed unmounts in loop");
101 
102 static struct krate krate_rename = { 1 };
103 
104 /*
105  * Virtual File System System Calls
106  */
107 
108 /*
109  * Mount a file system.
110  *
111  * mount_args(char *type, char *path, int flags, caddr_t data)
112  *
113  * MPALMOSTSAFE
114  */
115 int
116 sys_mount(struct sysmsg *sysmsg, const struct mount_args *uap)
117 {
118 	struct thread *td = curthread;
119 	struct vnode *vp;
120 	struct nchandle nch;
121 	struct mount *mp, *nullmp;
122 	struct vfsconf *vfsp;
123 	int error, flag = 0, flag2 = 0;
124 	int hasmount;
125 	int priv = 0;
126 	int flags = uap->flags;
127 	struct vattr va;
128 	struct nlookupdata nd;
129 	char fstypename[MFSNAMELEN];
130 	struct ucred *cred;
131 
132 	cred = td->td_ucred;
133 
134 	/* We do not allow user mounts inside a jail for now */
135 	if (usermount && jailed(cred)) {
136 		error = EPERM;
137 		goto done;
138 	}
139 
140 	/*
141 	 * Extract the file system type. We need to know this early, to take
142 	 * appropriate actions for jails and nullfs mounts.
143 	 */
144         if ((error = copyinstr(uap->type, fstypename, MFSNAMELEN, NULL)) != 0)
145 		goto done;
146 
147 	/*
148 	 * Select the correct priv according to the file system type.
149 	 */
150 	priv = get_fspriv(fstypename);
151 
152 	if (usermount == 0 && (error = priv_check(td, priv)))
153 		goto done;
154 
155 	/*
156 	 * Do not allow NFS export by non-root users.
157 	 */
158 	if (flags & MNT_EXPORTED) {
159 		error = priv_check(td, priv);
160 		if (error)
161 			goto done;
162 	}
163 	/*
164 	 * Silently enforce MNT_NOSUID and MNT_NODEV for non-root users
165 	 */
166 	if (priv_check(td, priv))
167 		flags |= MNT_NOSUID | MNT_NODEV;
168 
169 	/*
170 	 * Lookup the requested path and extract the nch and vnode.
171 	 */
172 	error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
173 	if (error == 0) {
174 		if ((error = nlookup(&nd)) == 0) {
175 			if (nd.nl_nch.ncp->nc_vp == NULL)
176 				error = ENOENT;
177 		}
178 	}
179 	if (error) {
180 		nlookup_done(&nd);
181 		goto done;
182 	}
183 
184 	/*
185 	 * If the target filesystem is resolved via a nullfs mount, then
186 	 * nd.nl_nch.mount will be pointing to the nullfs mount structure
187 	 * instead of the target file system. We need it in case we are
188 	 * doing an update.
189 	 */
190 	nullmp = nd.nl_nch.mount;
191 
192 	/*
193 	 * Extract the locked+refd ncp and cleanup the nd structure
194 	 */
195 	nch = nd.nl_nch;
196 	cache_zero(&nd.nl_nch);
197 	nlookup_done(&nd);
198 
199 	if ((nch.ncp->nc_flag & NCF_ISMOUNTPT) &&
200 	    (mp = cache_findmount(&nch)) != NULL) {
201 		cache_dropmount(mp);
202 		hasmount = 1;
203 	} else {
204 		hasmount = 0;
205 	}
206 
207 
208 	/*
209 	 * now we have the locked ref'd nch and unreferenced vnode.
210 	 */
211 	vp = nch.ncp->nc_vp;
212 	if ((error = vget(vp, LK_EXCLUSIVE)) != 0) {
213 		cache_put(&nch);
214 		goto done;
215 	}
216 	cache_unlock(&nch);
217 
218 	/*
219 	 * Now we have an unlocked ref'd nch and a locked ref'd vp
220 	 */
221 	if (flags & MNT_UPDATE) {
222 		if ((vp->v_flag & (VROOT|VPFSROOT)) == 0) {
223 			cache_drop(&nch);
224 			vput(vp);
225 			error = EINVAL;
226 			goto done;
227 		}
228 
229 		if (strncmp(fstypename, "null", 5) == 0) {
230 			KKASSERT(nullmp);
231 			mp = nullmp;
232 		} else {
233 			mp = vp->v_mount;
234 		}
235 
236 		flag = mp->mnt_flag;
237 		flag2 = mp->mnt_kern_flag;
238 		/*
239 		 * We only allow the filesystem to be reloaded if it
240 		 * is currently mounted read-only.
241 		 */
242 		if ((flags & MNT_RELOAD) &&
243 		    ((mp->mnt_flag & MNT_RDONLY) == 0)) {
244 			cache_drop(&nch);
245 			vput(vp);
246 			error = EOPNOTSUPP;	/* Needs translation */
247 			goto done;
248 		}
249 		/*
250 		 * Only root, or the user that did the original mount is
251 		 * permitted to update it.
252 		 */
253 		if (mp->mnt_stat.f_owner != cred->cr_uid &&
254 		    (error = priv_check(td, priv))) {
255 			cache_drop(&nch);
256 			vput(vp);
257 			goto done;
258 		}
259 		if (vfs_busy(mp, LK_NOWAIT)) {
260 			cache_drop(&nch);
261 			vput(vp);
262 			error = EBUSY;
263 			goto done;
264 		}
265 		if (hasmount) {
266 			cache_drop(&nch);
267 			vfs_unbusy(mp);
268 			vput(vp);
269 			error = EBUSY;
270 			goto done;
271 		}
272 		mp->mnt_flag |= flags & (MNT_RELOAD | MNT_FORCE | MNT_UPDATE);
273 		lwkt_gettoken(&mp->mnt_token);
274 		vn_unlock(vp);
275 		vfsp = mp->mnt_vfc;
276 		goto update;
277 	}
278 
279 	/*
280 	 * If the user is not root, ensure that they own the directory
281 	 * onto which we are attempting to mount.
282 	 */
283 	if ((error = VOP_GETATTR(vp, &va)) ||
284 	    (va.va_uid != cred->cr_uid &&
285 	     (error = priv_check(td, priv)))) {
286 		cache_drop(&nch);
287 		vput(vp);
288 		goto done;
289 	}
290 	if ((error = vinvalbuf(vp, V_SAVE, 0, 0)) != 0) {
291 		cache_drop(&nch);
292 		vput(vp);
293 		goto done;
294 	}
295 	if (vp->v_type != VDIR) {
296 		cache_drop(&nch);
297 		vput(vp);
298 		error = ENOTDIR;
299 		goto done;
300 	}
301 	if (vp->v_mount->mnt_kern_flag & MNTK_NOSTKMNT) {
302 		cache_drop(&nch);
303 		vput(vp);
304 		error = EPERM;
305 		goto done;
306 	}
307 	vfsp = vfsconf_find_by_name(fstypename);
308 	if (vfsp == NULL) {
309 		linker_file_t lf;
310 
311 		/* Only load modules for root (very important!) */
312 		if ((error = priv_check(td, PRIV_ROOT)) != 0) {
313 			cache_drop(&nch);
314 			vput(vp);
315 			goto done;
316 		}
317 		error = linker_load_file(fstypename, &lf);
318 		if (error || lf == NULL) {
319 			cache_drop(&nch);
320 			vput(vp);
321 			if (lf == NULL)
322 				error = ENODEV;
323 			goto done;
324 		}
325 		lf->userrefs++;
326 		/* lookup again, see if the VFS was loaded */
327 		vfsp = vfsconf_find_by_name(fstypename);
328 		if (vfsp == NULL) {
329 			lf->userrefs--;
330 			linker_file_unload(lf);
331 			cache_drop(&nch);
332 			vput(vp);
333 			error = ENODEV;
334 			goto done;
335 		}
336 	}
337 	if (hasmount) {
338 		cache_drop(&nch);
339 		vput(vp);
340 		error = EBUSY;
341 		goto done;
342 	}
343 
344 	/*
345 	 * Allocate and initialize the filesystem.
346 	 */
347 	mp = kmalloc(sizeof(struct mount), M_MOUNT, M_ZERO|M_WAITOK);
348 	mount_init(mp, vfsp->vfc_vfsops);
349 	vfs_busy(mp, LK_NOWAIT);
350 	mp->mnt_vfc = vfsp;
351 	mp->mnt_pbuf_count = nswbuf_kva / NSWBUF_SPLIT;
352 	vfsp->vfc_refcount++;
353 	mp->mnt_stat.f_type = vfsp->vfc_typenum;
354 	mp->mnt_flag |= vfsp->vfc_flags & MNT_VISFLAGMASK;
355 	strncpy(mp->mnt_stat.f_fstypename, vfsp->vfc_name, MFSNAMELEN);
356 	mp->mnt_stat.f_owner = cred->cr_uid;
357 	lwkt_gettoken(&mp->mnt_token);
358 	vn_unlock(vp);
359 update:
360 	/*
361 	 * (per-mount token acquired at this point)
362 	 *
363 	 * Set the mount level flags.
364 	 */
365 	if (flags & MNT_RDONLY)
366 		mp->mnt_flag |= MNT_RDONLY;
367 	else if (mp->mnt_flag & MNT_RDONLY)
368 		mp->mnt_kern_flag |= MNTK_WANTRDWR;
369 	mp->mnt_flag &=~ (MNT_NOSUID | MNT_NOEXEC | MNT_NODEV |
370 	    MNT_SYNCHRONOUS | MNT_ASYNC | MNT_NOATIME |
371 	    MNT_NOSYMFOLLOW | MNT_IGNORE | MNT_TRIM |
372 	    MNT_NOCLUSTERR | MNT_NOCLUSTERW | MNT_SUIDDIR |
373 	    MNT_AUTOMOUNTED);
374 	mp->mnt_flag |= flags & (MNT_NOSUID | MNT_NOEXEC |
375 	    MNT_NODEV | MNT_SYNCHRONOUS | MNT_ASYNC | MNT_FORCE |
376 	    MNT_NOSYMFOLLOW | MNT_IGNORE | MNT_TRIM |
377 	    MNT_NOATIME | MNT_NOCLUSTERR | MNT_NOCLUSTERW | MNT_SUIDDIR |
378 	    MNT_AUTOMOUNTED);
379 
380 	/*
381 	 * Pre-set the mount's ALL_MPSAFE flags if specified in the vfsconf.
382 	 * This way the initial VFS_MOUNT() call will also be MPSAFE.
383 	 */
384 	if (vfsp->vfc_flags & VFCF_MPSAFE)
385 		mp->mnt_kern_flag |= MNTK_ALL_MPSAFE;
386 
387 	/*
388 	 * Mount the filesystem.
389 	 * XXX The final recipients of VFS_MOUNT just overwrite the ndp they
390 	 * get.
391 	 */
392 	if (mp->mnt_flag & MNT_UPDATE) {
393 		error = VFS_MOUNT(mp, uap->path, uap->data, cred);
394 		if (mp->mnt_kern_flag & MNTK_WANTRDWR)
395 			mp->mnt_flag &= ~MNT_RDONLY;
396 		mp->mnt_flag &=~ (MNT_UPDATE | MNT_RELOAD | MNT_FORCE);
397 		mp->mnt_kern_flag &=~ MNTK_WANTRDWR;
398 		if (error) {
399 			mp->mnt_flag = flag;
400 			mp->mnt_kern_flag = flag2;
401 		}
402 		lwkt_reltoken(&mp->mnt_token);
403 		vfs_unbusy(mp);
404 		vrele(vp);
405 		cache_drop(&nch);
406 		goto done;
407 	}
408 	mp->mnt_ncmounton = nch;
409 	error = VFS_MOUNT(mp, uap->path, uap->data, cred);
410 	vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
411 
412 	/*
413 	 * Put the new filesystem on the mount list after root.  The mount
414 	 * point gets its own mnt_ncmountpt (unless the VFS already set one
415 	 * up) which represents the root of the mount.  The lookup code
416 	 * detects the mount point going forward and checks the root of
417 	 * the mount going backwards.
418 	 *
419 	 * It is not necessary to invalidate or purge the vnode underneath
420 	 * because elements under the mount will be given their own glue
421 	 * namecache record.
422 	 */
423 	if (!error) {
424 		if (mp->mnt_ncmountpt.ncp == NULL) {
425 			/*
426 			 * Allocate, then unlock, but leave the ref intact.
427 			 * This is the mnt_refs (1) that we will retain
428 			 * through to the unmount.
429 			 */
430 			cache_allocroot(&mp->mnt_ncmountpt, mp, NULL);
431 			cache_unlock(&mp->mnt_ncmountpt);
432 		}
433 		vn_unlock(vp);
434 		cache_lock(&nch);
435 		nch.ncp->nc_flag |= NCF_ISMOUNTPT;
436 		cache_unlock(&nch);
437 		cache_ismounting(mp);
438 		vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
439 
440 		mountlist_insert(mp, MNTINS_LAST);
441 		vn_unlock(vp);
442 		checkdirs(&mp->mnt_ncmounton, &mp->mnt_ncmountpt);
443 		error = vfs_allocate_syncvnode(mp);
444 		lwkt_reltoken(&mp->mnt_token);
445 		vfs_unbusy(mp);
446 		error = VFS_START(mp, 0);
447 		vrele(vp);
448 		KNOTE(&fs_klist, VQ_MOUNT);
449 	} else {
450 		bzero(&mp->mnt_ncmounton, sizeof(mp->mnt_ncmounton));
451 		vn_syncer_thr_stop(mp);
452 		vfs_rm_vnodeops(mp, NULL, &mp->mnt_vn_coherency_ops);
453 		vfs_rm_vnodeops(mp, NULL, &mp->mnt_vn_journal_ops);
454 		vfs_rm_vnodeops(mp, NULL, &mp->mnt_vn_norm_ops);
455 		vfs_rm_vnodeops(mp, NULL, &mp->mnt_vn_spec_ops);
456 		vfs_rm_vnodeops(mp, NULL, &mp->mnt_vn_fifo_ops);
457 		if (mp->mnt_cred) {
458 			crfree(mp->mnt_cred);
459 			mp->mnt_cred = NULL;
460 		}
461 		mp->mnt_vfc->vfc_refcount--;
462 		lwkt_reltoken(&mp->mnt_token);
463 		vfs_unbusy(mp);
464 		kfree(mp, M_MOUNT);
465 		cache_drop(&nch);
466 		vput(vp);
467 	}
468 done:
469 	return (error);
470 }
471 
472 /*
473  * Scan all active processes to see if any of them have a current
474  * or root directory onto which the new filesystem has just been
475  * mounted. If so, replace them with the new mount point.
476  *
477  * Both old_nch and new_nch are ref'd on call but not locked.
478  * new_nch must be temporarily locked so it can be associated with the
479  * vnode representing the root of the mount point.
480  */
481 struct checkdirs_info {
482 	struct nchandle old_nch;
483 	struct nchandle new_nch;
484 	struct vnode *old_vp;
485 	struct vnode *new_vp;
486 };
487 
488 static int checkdirs_callback(struct proc *p, void *data);
489 
490 static void
491 checkdirs(struct nchandle *old_nch, struct nchandle *new_nch)
492 {
493 	struct checkdirs_info info;
494 	struct vnode *olddp;
495 	struct vnode *newdp;
496 	struct mount *mp;
497 
498 	/*
499 	 * If the old mount point's vnode has a usecount of 1, it is not
500 	 * being held as a descriptor anywhere.
501 	 */
502 	olddp = old_nch->ncp->nc_vp;
503 	if (olddp == NULL || VREFCNT(olddp) == 1)
504 		return;
505 
506 	/*
507 	 * Force the root vnode of the new mount point to be resolved
508 	 * so we can update any matching processes.
509 	 */
510 	mp = new_nch->mount;
511 	if (VFS_ROOT(mp, &newdp))
512 		panic("mount: lost mount");
513 	vn_unlock(newdp);
514 	cache_lock(new_nch);
515 	vn_lock(newdp, LK_EXCLUSIVE | LK_RETRY);
516 	cache_setunresolved(new_nch);
517 	cache_setvp(new_nch, newdp);
518 	cache_unlock(new_nch);
519 
520 	/*
521 	 * Special handling of the root node
522 	 */
523 	if (rootvnode == olddp) {
524 		vref(newdp);
525 		vfs_cache_setroot(newdp, cache_hold(new_nch));
526 	}
527 
528 	/*
529 	 * Pass newdp separately so the callback does not have to access
530 	 * it via new_nch->ncp->nc_vp.
531 	 */
532 	info.old_nch = *old_nch;
533 	info.new_nch = *new_nch;
534 	info.new_vp = newdp;
535 	allproc_scan(checkdirs_callback, &info, 0);
536 	vput(newdp);
537 }
538 
539 /*
540  * NOTE: callback is not MP safe because the scanned process's filedesc
541  * structure can be ripped out from under us, amoung other things.
542  */
543 static int
544 checkdirs_callback(struct proc *p, void *data)
545 {
546 	struct checkdirs_info *info = data;
547 	struct filedesc *fdp;
548 	struct nchandle ncdrop1;
549 	struct nchandle ncdrop2;
550 	struct vnode *vprele1;
551 	struct vnode *vprele2;
552 
553 	if ((fdp = p->p_fd) != NULL) {
554 		cache_zero(&ncdrop1);
555 		cache_zero(&ncdrop2);
556 		vprele1 = NULL;
557 		vprele2 = NULL;
558 
559 		/*
560 		 * MPUNSAFE - XXX fdp can be pulled out from under a
561 		 * foreign process.
562 		 *
563 		 * A shared filedesc is ok, we don't have to copy it
564 		 * because we are making this change globally.
565 		 */
566 		spin_lock(&fdp->fd_spin);
567 		if (fdp->fd_ncdir.mount == info->old_nch.mount &&
568 		    fdp->fd_ncdir.ncp == info->old_nch.ncp) {
569 			vprele1 = fdp->fd_cdir;
570 			vref(info->new_vp);
571 			fdp->fd_cdir = info->new_vp;
572 			ncdrop1 = fdp->fd_ncdir;
573 			cache_copy(&info->new_nch, &fdp->fd_ncdir);
574 		}
575 		if (fdp->fd_nrdir.mount == info->old_nch.mount &&
576 		    fdp->fd_nrdir.ncp == info->old_nch.ncp) {
577 			vprele2 = fdp->fd_rdir;
578 			vref(info->new_vp);
579 			fdp->fd_rdir = info->new_vp;
580 			ncdrop2 = fdp->fd_nrdir;
581 			cache_copy(&info->new_nch, &fdp->fd_nrdir);
582 		}
583 		spin_unlock(&fdp->fd_spin);
584 		if (ncdrop1.ncp)
585 			cache_drop(&ncdrop1);
586 		if (ncdrop2.ncp)
587 			cache_drop(&ncdrop2);
588 		if (vprele1)
589 			vrele(vprele1);
590 		if (vprele2)
591 			vrele(vprele2);
592 	}
593 	return(0);
594 }
595 
596 /*
597  * Unmount a file system.
598  *
599  * Note: unmount takes a path to the vnode mounted on as argument,
600  * not special file (as before).
601  *
602  * umount_args(char *path, int flags)
603  *
604  * MPALMOSTSAFE
605  */
606 int
607 sys_unmount(struct sysmsg *sysmsg, const struct unmount_args *uap)
608 {
609 	struct thread *td = curthread;
610 	struct proc *p __debugvar = td->td_proc;
611 	struct mount *mp = NULL;
612 	struct nlookupdata nd;
613 	char fstypename[MFSNAMELEN];
614 	int priv = 0;
615 	int error;
616 	struct ucred *cred;
617 
618 	cred = td->td_ucred;
619 
620 	KKASSERT(p);
621 
622 	/* We do not allow user umounts inside a jail for now */
623 	if (usermount && jailed(cred)) {
624 		error = EPERM;
625 		goto done;
626 	}
627 
628 	error = nlookup_init(&nd, uap->path, UIO_USERSPACE,
629 			     NLC_FOLLOW | NLC_IGNBADDIR);
630 	if (error == 0)
631 		error = nlookup(&nd);
632 	if (error)
633 		goto out;
634 
635 	mp = nd.nl_nch.mount;
636 
637 	/* Figure out the fsname in order to select proper privs */
638 	ksnprintf(fstypename, MFSNAMELEN, "%s", mp->mnt_vfc->vfc_name);
639 	priv = get_fspriv(fstypename);
640 
641 	if (usermount == 0 && (error = priv_check(td, priv))) {
642 		nlookup_done(&nd);
643 		goto done;
644 	}
645 
646 	/*
647 	 * Only root, or the user that did the original mount is
648 	 * permitted to unmount this filesystem.
649 	 */
650 	if ((mp->mnt_stat.f_owner != td->td_ucred->cr_uid) &&
651 	    (error = priv_check(td, priv)))
652 		goto out;
653 
654 	/*
655 	 * Don't allow unmounting the root file system.
656 	 */
657 	if (mp->mnt_flag & MNT_ROOTFS) {
658 		error = EINVAL;
659 		goto out;
660 	}
661 
662 	/*
663 	 * Must be the root of the filesystem
664 	 */
665 	if (nd.nl_nch.ncp != mp->mnt_ncmountpt.ncp) {
666 		error = EINVAL;
667 		goto out;
668 	}
669 
670 	/* Check if this mount belongs to this prison */
671 	if (jailed(cred) && mp->mnt_cred && (!mp->mnt_cred->cr_prison ||
672 		mp->mnt_cred->cr_prison != cred->cr_prison)) {
673 		kprintf("mountpoint %s does not belong to this jail\n",
674 		    uap->path);
675 		error = EPERM;
676 		goto out;
677 	}
678 
679 	/*
680 	 * If no error try to issue the unmount.  We lose our cache
681 	 * ref when we call nlookup_done so we must hold the mount point
682 	 * to prevent use-after-free races.
683 	 */
684 out:
685 	if (error == 0) {
686 		mount_hold(mp);
687 		nlookup_done(&nd);
688 		error = dounmount(mp, uap->flags, 0);
689 		mount_drop(mp);
690 	} else {
691 		nlookup_done(&nd);
692 	}
693 done:
694 	return (error);
695 }
696 
697 /*
698  * Do the actual file system unmount (interlocked against the mountlist
699  * token and mp->mnt_token).
700  */
701 static int
702 dounmount_interlock(struct mount *mp)
703 {
704 	if (mp->mnt_kern_flag & MNTK_UNMOUNT)
705 		return (EBUSY);
706 	mp->mnt_kern_flag |= MNTK_UNMOUNT;
707 	return(0);
708 }
709 
710 static int
711 unmount_allproc_cb(struct proc *p, void *arg)
712 {
713 	struct mount *mp;
714 
715 	if (p->p_textnch.ncp == NULL)
716 		return 0;
717 
718 	mp = (struct mount *)arg;
719 	if (p->p_textnch.mount == mp)
720 		cache_drop(&p->p_textnch);
721 
722 	return 0;
723 }
724 
725 /*
726  * The guts of the unmount code.  The mount owns one ref and one hold
727  * count.  If we successfully interlock the unmount, those refs are ours.
728  * (The ref is from mnt_ncmountpt).
729  *
730  * When halting we shortcut certain mount types such as devfs by not actually
731  * issuing the VFS_SYNC() or VFS_UNMOUNT().  They are still disconnected
732  * from the mountlist so higher-level filesytems can unmount cleanly.
733  *
734  * The mount types that allow QUICKHALT are: devfs, tmpfs, procfs.
735  */
736 int
737 dounmount(struct mount *mp, int flags, int halting)
738 {
739 	struct namecache *ncp;
740 	struct nchandle nch;
741 	struct vnode *vp;
742 	int error;
743 	int async_flag;
744 	int lflags;
745 	int freeok = 1;
746 	int hadsyncer = 0;
747 	int retry;
748 	int quickhalt;
749 
750 	lwkt_gettoken(&mp->mnt_token);
751 
752 	/*
753 	 * When halting, certain mount points can essentially just
754 	 * be unhooked and otherwise ignored.
755 	 */
756 	if (halting && (mp->mnt_kern_flag & MNTK_QUICKHALT)) {
757 		quickhalt = 1;
758 		freeok = 0;
759 	} else {
760 		quickhalt = 0;
761 	}
762 
763 
764 	/*
765 	 * Exclusive access for unmounting purposes.
766 	 */
767 	if ((error = mountlist_interlock(dounmount_interlock, mp)) != 0)
768 		goto out;
769 
770 	/*
771 	 * We now 'own' the last mp->mnt_refs
772 	 *
773 	 * Allow filesystems to detect that a forced unmount is in progress.
774 	 */
775 	if (flags & MNT_FORCE)
776 		mp->mnt_kern_flag |= MNTK_UNMOUNTF;
777 	lflags = LK_EXCLUSIVE | ((flags & MNT_FORCE) ? 0 : LK_TIMELOCK);
778 	error = lockmgr(&mp->mnt_lock, lflags);
779 	if (error) {
780 		mp->mnt_kern_flag &= ~(MNTK_UNMOUNT | MNTK_UNMOUNTF);
781 		if (mp->mnt_kern_flag & MNTK_MWAIT) {
782 			mp->mnt_kern_flag &= ~MNTK_MWAIT;
783 			wakeup(mp);
784 		}
785 		goto out;
786 	}
787 
788 	if (mp->mnt_flag & MNT_EXPUBLIC)
789 		vfs_setpublicfs(NULL, NULL, NULL);
790 
791 	vfs_msync(mp, MNT_WAIT);
792 	async_flag = mp->mnt_flag & MNT_ASYNC;
793 	mp->mnt_flag &=~ MNT_ASYNC;
794 
795 	/*
796 	 * Decomission our special mnt_syncer vnode.  This also stops
797 	 * the vnlru code.  If we are unable to unmount we recommission
798 	 * the vnode.
799 	 *
800 	 * Then sync the filesystem.
801 	 */
802 	if ((vp = mp->mnt_syncer) != NULL) {
803 		mp->mnt_syncer = NULL;
804 		atomic_set_int(&vp->v_refcnt, VREF_FINALIZE);
805 		vrele(vp);
806 		hadsyncer = 1;
807 	}
808 
809 	/*
810 	 * Sync normally-mounted filesystem.
811 	 */
812 	if (quickhalt == 0) {
813 		if ((mp->mnt_flag & MNT_RDONLY) == 0)
814 			VFS_SYNC(mp, MNT_WAIT);
815 	}
816 
817 	/*
818 	 * nchandle records ref the mount structure.  Expect a count of 1
819 	 * (our mount->mnt_ncmountpt).
820 	 *
821 	 * Scans can get temporary refs on a mountpoint (thought really
822 	 * heavy duty stuff like cache_findmount() do not).
823 	 */
824 	for (retry = 0; (retry < 10 || debug_unmount); ++retry) {
825 		/*
826 		 * Invalidate the namecache topology under the mount.
827 		 * nullfs mounts alias a real mount's namecache topology
828 		 * and it should not be invalidated in that case.
829 		 */
830 		if ((mp->mnt_kern_flag & MNTK_NCALIASED) == 0) {
831 			cache_lock(&mp->mnt_ncmountpt);
832 			cache_inval(&mp->mnt_ncmountpt,
833 				    CINV_DESTROY|CINV_CHILDREN);
834 			cache_unlock(&mp->mnt_ncmountpt);
835 		}
836 
837 		/*
838 		 * Clear pcpu caches
839 		 */
840 		cache_unmounting(mp);
841 		if (mp->mnt_refs != 1)
842 			cache_clearmntcache(mp);
843 
844 		/*
845 		 * Break out if we are good.  Don't count ncp refs if the
846 		 * mount is aliased.
847 		 */
848 		ncp = (mp->mnt_kern_flag & MNTK_NCALIASED) ?
849 		      NULL : mp->mnt_ncmountpt.ncp;
850 		if (mp->mnt_refs == 1 &&
851 		    (ncp == NULL || (ncp->nc_refs == 1 &&
852 				     TAILQ_FIRST(&ncp->nc_list) == NULL))) {
853 			break;
854 		}
855 
856 		/*
857 		 * If forcing the unmount, clean out any p->p_textnch
858 		 * nchandles that match this mount.
859 		 */
860 		if (flags & MNT_FORCE)
861 			allproc_scan(&unmount_allproc_cb, mp, 0);
862 
863 		/*
864 		 * Sleep and retry.
865 		 */
866 		tsleep(&mp->mnt_refs, 0, "mntbsy", hz / 10 + 1);
867 		if ((retry & 15) == 15) {
868 			mount_warning(mp,
869 				      "(%p) debug - retry %d, "
870 				      "%d namecache refs, %d mount refs",
871 				      mp, retry,
872 				      (ncp ? ncp->nc_refs - 1 : 0),
873 				      mp->mnt_refs - 1);
874 		}
875 	}
876 
877 	error = 0;
878 	ncp = (mp->mnt_kern_flag & MNTK_NCALIASED) ?
879 	      NULL : mp->mnt_ncmountpt.ncp;
880 	if (mp->mnt_refs != 1 ||
881 	    (ncp != NULL && (ncp->nc_refs != 1 ||
882 			     TAILQ_FIRST(&ncp->nc_list)))) {
883 		mount_warning(mp,
884 			      "(%p): %d namecache refs, %d mount refs "
885 			      "still present",
886 			      mp,
887 			      (ncp ? ncp->nc_refs - 1 : 0),
888 			      mp->mnt_refs - 1);
889 		if (flags & MNT_FORCE) {
890 			freeok = 0;
891 			mount_warning(mp, "forcing unmount\n");
892 		} else {
893 			error = EBUSY;
894 		}
895 	}
896 
897 	/*
898 	 * So far so good, sync the filesystem once more and
899 	 * call the VFS unmount code if the sync succeeds.
900 	 */
901 	if (error == 0 && quickhalt == 0) {
902 		if (mp->mnt_flag & MNT_RDONLY) {
903 			error = VFS_UNMOUNT(mp, flags);
904 		} else {
905 			error = VFS_SYNC(mp, MNT_WAIT);
906 			if (error == 0 ||		/* no error */
907 			    error == EOPNOTSUPP ||	/* no sync avail */
908 			    (flags & MNT_FORCE)) {	/* force anyway */
909 				error = VFS_UNMOUNT(mp, flags);
910 			}
911 		}
912 		if (error) {
913 			mount_warning(mp,
914 				      "(%p) unmount: vfs refused to unmount, "
915 				      "error %d",
916 				      mp, error);
917 		}
918 	}
919 
920 	/*
921 	 * If an error occurred we can still recover, restoring the
922 	 * syncer vnode and misc flags.
923 	 */
924 	if (error) {
925 		if (mp->mnt_syncer == NULL && hadsyncer)
926 			vfs_allocate_syncvnode(mp);
927 		mp->mnt_kern_flag &= ~(MNTK_UNMOUNT | MNTK_UNMOUNTF);
928 		mp->mnt_flag |= async_flag;
929 		lockmgr(&mp->mnt_lock, LK_RELEASE);
930 		if (mp->mnt_kern_flag & MNTK_MWAIT) {
931 			mp->mnt_kern_flag &= ~MNTK_MWAIT;
932 			wakeup(mp);
933 		}
934 		goto out;
935 	}
936 	/*
937 	 * Clean up any journals still associated with the mount after
938 	 * filesystem activity has ceased.
939 	 */
940 	journal_remove_all_journals(mp,
941 	    ((flags & MNT_FORCE) ? MC_JOURNAL_STOP_IMM : 0));
942 
943 	mountlist_remove(mp);
944 
945 	/*
946 	 * Remove any installed vnode ops here so the individual VFSs don't
947 	 * have to.
948 	 *
949 	 * mnt_refs should go to zero when we scrap mnt_ncmountpt.
950 	 *
951 	 * When quickhalting we have to keep these intact because the
952 	 * underlying vnodes have not been destroyed, and some might be
953 	 * dirty.
954 	 */
955 	if (quickhalt == 0) {
956 		vfs_rm_vnodeops(mp, NULL, &mp->mnt_vn_coherency_ops);
957 		vfs_rm_vnodeops(mp, NULL, &mp->mnt_vn_journal_ops);
958 		vfs_rm_vnodeops(mp, NULL, &mp->mnt_vn_norm_ops);
959 		vfs_rm_vnodeops(mp, NULL, &mp->mnt_vn_spec_ops);
960 		vfs_rm_vnodeops(mp, NULL, &mp->mnt_vn_fifo_ops);
961 	}
962 
963 	if (mp->mnt_ncmountpt.ncp != NULL) {
964 		nch = mp->mnt_ncmountpt;
965 		cache_zero(&mp->mnt_ncmountpt);
966 		cache_clrmountpt(&nch);
967 		cache_drop(&nch);
968 	}
969 	if (mp->mnt_ncmounton.ncp != NULL) {
970 		cache_unmounting(mp);
971 		nch = mp->mnt_ncmounton;
972 		cache_zero(&mp->mnt_ncmounton);
973 		cache_clrmountpt(&nch);
974 		cache_drop(&nch);
975 	}
976 
977 	if (mp->mnt_cred) {
978 		crfree(mp->mnt_cred);
979 		mp->mnt_cred = NULL;
980 	}
981 
982 	mp->mnt_vfc->vfc_refcount--;
983 
984 	/*
985 	 * If not quickhalting the mount, we expect there to be no
986 	 * vnodes left.
987 	 */
988 	if (quickhalt == 0 && !TAILQ_EMPTY(&mp->mnt_nvnodelist))
989 		panic("unmount: dangling vnode");
990 
991 	/*
992 	 * Release the lock
993 	 */
994 	lockmgr(&mp->mnt_lock, LK_RELEASE);
995 	if (mp->mnt_kern_flag & MNTK_MWAIT) {
996 		mp->mnt_kern_flag &= ~MNTK_MWAIT;
997 		wakeup(mp);
998 	}
999 
1000 	/*
1001 	 * If we reach here and freeok != 0 we must free the mount.
1002 	 * mnt_refs should already have dropped to 0, so if it is not
1003 	 * zero we must cycle the caches and wait.
1004 	 *
1005 	 * When we are satisfied that the mount has disconnected we can
1006 	 * drop the hold on the mp that represented the mount (though the
1007 	 * caller might actually have another, so the caller's drop may
1008 	 * do the actual free).
1009 	 */
1010 	if (freeok) {
1011 		if (mp->mnt_refs > 0)
1012 			cache_clearmntcache(mp);
1013 		while (mp->mnt_refs > 0) {
1014 			cache_unmounting(mp);
1015 			wakeup(mp);
1016 			tsleep(&mp->mnt_refs, 0, "umntrwait", hz / 10 + 1);
1017 			cache_clearmntcache(mp);
1018 		}
1019 		lwkt_reltoken(&mp->mnt_token);
1020 		mount_drop(mp);
1021 		mp = NULL;
1022 	} else {
1023 		cache_clearmntcache(mp);
1024 	}
1025 	error = 0;
1026 	KNOTE(&fs_klist, VQ_UNMOUNT);
1027 out:
1028 	if (mp)
1029 		lwkt_reltoken(&mp->mnt_token);
1030 	return (error);
1031 }
1032 
1033 static
1034 void
1035 mount_warning(struct mount *mp, const char *ctl, ...)
1036 {
1037 	char *ptr;
1038 	char *buf;
1039 	__va_list va;
1040 
1041 	__va_start(va, ctl);
1042 	if (cache_fullpath(NULL, &mp->mnt_ncmounton, NULL,
1043 			   &ptr, &buf, 0) == 0) {
1044 		kprintf("unmount(%s): ", ptr);
1045 		kvprintf(ctl, va);
1046 		kprintf("\n");
1047 		kfree(buf, M_TEMP);
1048 	} else {
1049 		kprintf("unmount(%p", mp);
1050 		if (mp->mnt_ncmounton.ncp && mp->mnt_ncmounton.ncp->nc_name)
1051 			kprintf(",%s", mp->mnt_ncmounton.ncp->nc_name);
1052 		kprintf("): ");
1053 		kvprintf(ctl, va);
1054 		kprintf("\n");
1055 	}
1056 	__va_end(va);
1057 }
1058 
1059 /*
1060  * Shim cache_fullpath() to handle the case where a process is chrooted into
1061  * a subdirectory of a mount.  In this case if the root mount matches the
1062  * process root directory's mount we have to specify the process's root
1063  * directory instead of the mount point, because the mount point might
1064  * be above the root directory.
1065  */
1066 static
1067 int
1068 mount_path(struct proc *p, struct mount *mp, char **rb, char **fb)
1069 {
1070 	struct nchandle *nch;
1071 
1072 	if (p && p->p_fd->fd_nrdir.mount == mp)
1073 		nch = &p->p_fd->fd_nrdir;
1074 	else
1075 		nch = &mp->mnt_ncmountpt;
1076 	return(cache_fullpath(p, nch, NULL, rb, fb, 0));
1077 }
1078 
1079 /*
1080  * Sync each mounted filesystem.
1081  */
1082 
1083 #ifdef DEBUG
1084 static int syncprt = 0;
1085 SYSCTL_INT(_debug, OID_AUTO, syncprt, CTLFLAG_RW, &syncprt, 0, "");
1086 #endif /* DEBUG */
1087 
1088 static int sync_callback(struct mount *mp, void *data);
1089 
1090 int
1091 sys_sync(struct sysmsg *sysmsg, const struct sync_args *uap)
1092 {
1093 	mountlist_scan(sync_callback, NULL, MNTSCAN_FORWARD);
1094 	return (0);
1095 }
1096 
1097 static
1098 int
1099 sync_callback(struct mount *mp, void *data __unused)
1100 {
1101 	int asyncflag;
1102 
1103 	if ((mp->mnt_flag & MNT_RDONLY) == 0) {
1104 		lwkt_gettoken(&mp->mnt_token);
1105 		asyncflag = mp->mnt_flag & MNT_ASYNC;
1106 		mp->mnt_flag &= ~MNT_ASYNC;
1107 		lwkt_reltoken(&mp->mnt_token);
1108 		vfs_msync(mp, MNT_NOWAIT);
1109 		VFS_SYNC(mp, MNT_NOWAIT);
1110 		lwkt_gettoken(&mp->mnt_token);
1111 		mp->mnt_flag |= asyncflag;
1112 		lwkt_reltoken(&mp->mnt_token);
1113 	}
1114 	return(0);
1115 }
1116 
1117 /* XXX PRISON: could be per prison flag */
1118 static int prison_quotas;
1119 #if 0
1120 SYSCTL_INT(_kern_prison, OID_AUTO, quotas, CTLFLAG_RW, &prison_quotas, 0, "");
1121 #endif
1122 
1123 /*
1124  *  quotactl_args(char *path, int fcmd, int uid, caddr_t arg)
1125  *
1126  * Change filesystem quotas.
1127  *
1128  * MPALMOSTSAFE
1129  */
1130 int
1131 sys_quotactl(struct sysmsg *sysmsg, const struct quotactl_args *uap)
1132 {
1133 	struct nlookupdata nd;
1134 	struct thread *td;
1135 	struct mount *mp;
1136 	int error;
1137 
1138 	td = curthread;
1139 	if (td->td_ucred->cr_prison && !prison_quotas) {
1140 		error = EPERM;
1141 		goto done;
1142 	}
1143 
1144 	error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
1145 	if (error == 0)
1146 		error = nlookup(&nd);
1147 	if (error == 0) {
1148 		mp = nd.nl_nch.mount;
1149 		error = VFS_QUOTACTL(mp, uap->cmd, uap->uid,
1150 				    uap->arg, nd.nl_cred);
1151 	}
1152 	nlookup_done(&nd);
1153 done:
1154 	return (error);
1155 }
1156 
1157 /*
1158  * mountctl(char *path, int op, int fd, const void *ctl, int ctllen,
1159  *		void *buf, int buflen)
1160  *
1161  * This function operates on a mount point and executes the specified
1162  * operation using the specified control data, and possibly returns data.
1163  *
1164  * The actual number of bytes stored in the result buffer is returned, 0
1165  * if none, otherwise an error is returned.
1166  *
1167  * MPALMOSTSAFE
1168  */
1169 int
1170 sys_mountctl(struct sysmsg *sysmsg, const struct mountctl_args *uap)
1171 {
1172 	struct thread *td = curthread;
1173 	struct file *fp;
1174 	void *ctl = NULL;
1175 	void *buf = NULL;
1176 	char *path = NULL;
1177 	int error;
1178 
1179 	/*
1180 	 * Sanity and permissions checks.  We must be root.
1181 	 */
1182 	if (td->td_ucred->cr_prison != NULL)
1183 		return (EPERM);
1184 	if ((uap->op != MOUNTCTL_MOUNTFLAGS) &&
1185 	    (error = priv_check(td, PRIV_ROOT)) != 0)
1186 		return (error);
1187 
1188 	/*
1189 	 * Argument length checks
1190 	 */
1191 	if (uap->ctllen < 0 || uap->ctllen > 1024)
1192 		return (EINVAL);
1193 	if (uap->buflen < 0 || uap->buflen > 16 * 1024)
1194 		return (EINVAL);
1195 	if (uap->path == NULL)
1196 		return (EINVAL);
1197 
1198 	/*
1199 	 * Allocate the necessary buffers and copyin data
1200 	 */
1201 	path = objcache_get(namei_oc, M_WAITOK);
1202 	error = copyinstr(uap->path, path, MAXPATHLEN, NULL);
1203 	if (error)
1204 		goto done;
1205 
1206 	if (uap->ctllen) {
1207 		ctl = kmalloc(uap->ctllen + 1, M_TEMP, M_WAITOK|M_ZERO);
1208 		error = copyin(uap->ctl, ctl, uap->ctllen);
1209 		if (error)
1210 			goto done;
1211 	}
1212 	if (uap->buflen)
1213 		buf = kmalloc(uap->buflen + 1, M_TEMP, M_WAITOK|M_ZERO);
1214 
1215 	/*
1216 	 * Validate the descriptor
1217 	 */
1218 	if (uap->fd >= 0) {
1219 		fp = holdfp(td, uap->fd, -1);
1220 		if (fp == NULL) {
1221 			error = EBADF;
1222 			goto done;
1223 		}
1224 	} else {
1225 		fp = NULL;
1226 	}
1227 
1228 	/*
1229 	 * Execute the internal kernel function and clean up.
1230 	 */
1231 	error = kern_mountctl(path, uap->op, fp, ctl, uap->ctllen,
1232 			      buf, uap->buflen, &sysmsg->sysmsg_result);
1233 	if (fp)
1234 		dropfp(td, uap->fd, fp);
1235 	if (error == 0 && sysmsg->sysmsg_result > 0)
1236 		error = copyout(buf, uap->buf, sysmsg->sysmsg_result);
1237 done:
1238 	if (path)
1239 		objcache_put(namei_oc, path);
1240 	if (ctl)
1241 		kfree(ctl, M_TEMP);
1242 	if (buf)
1243 		kfree(buf, M_TEMP);
1244 	return (error);
1245 }
1246 
1247 /*
1248  * Execute a mount control operation by resolving the path to a mount point
1249  * and calling vop_mountctl().
1250  *
1251  * Use the mount point from the nch instead of the vnode so nullfs mounts
1252  * can properly spike the VOP.
1253  */
1254 int
1255 kern_mountctl(const char *path, int op, struct file *fp,
1256 		const void *ctl, int ctllen,
1257 		void *buf, int buflen, int *res)
1258 {
1259 	struct vnode *vp;
1260 	struct nlookupdata nd;
1261 	struct nchandle nch;
1262 	struct mount *mp;
1263 	int error;
1264 
1265 	*res = 0;
1266 	vp = NULL;
1267 	error = nlookup_init(&nd, path, UIO_SYSSPACE, NLC_FOLLOW);
1268 	if (error)
1269 		return (error);
1270 	error = nlookup(&nd);
1271 	if (error) {
1272 		nlookup_done(&nd);
1273 		return (error);
1274 	}
1275 	error = cache_vget(&nd.nl_nch, nd.nl_cred, LK_EXCLUSIVE, &vp);
1276 	if (error) {
1277 		nlookup_done(&nd);
1278 		return (error);
1279 	}
1280 
1281 	/*
1282 	 * Yes, all this is needed to use the nch.mount below, because
1283 	 * we must maintain a ref on the mount to avoid ripouts (e.g.
1284 	 * due to heavy mount/unmount use by synth or poudriere).
1285 	 */
1286 	nch = nd.nl_nch;
1287 	cache_zero(&nd.nl_nch);
1288 	cache_unlock(&nch);
1289 	nlookup_done(&nd);
1290 	vn_unlock(vp);
1291 
1292 	mp = nch.mount;
1293 
1294 	/*
1295 	 * Must be the root of the filesystem
1296 	 */
1297 	if ((vp->v_flag & (VROOT|VPFSROOT)) == 0) {
1298 		cache_drop(&nch);
1299 		vrele(vp);
1300 		return (EINVAL);
1301 	}
1302 	if (mp == NULL || mp->mnt_kern_flag & MNTK_UNMOUNT) {
1303 		kprintf("kern_mountctl: Warning, \"%s\" racing unmount\n",
1304 			path);
1305 		cache_drop(&nch);
1306 		vrele(vp);
1307 		return (EINVAL);
1308 	}
1309 	error = vop_mountctl(mp->mnt_vn_use_ops, vp, op, fp, ctl, ctllen,
1310 			     buf, buflen, res);
1311 	vrele(vp);
1312 	cache_drop(&nch);
1313 
1314 	return (error);
1315 }
1316 
1317 int
1318 kern_statfs(struct nlookupdata *nd, struct statfs *buf)
1319 {
1320 	struct thread *td = curthread;
1321 	struct proc *p = td->td_proc;
1322 	struct mount *mp;
1323 	struct statfs *sp;
1324 	char *fullpath, *freepath;
1325 	int error;
1326 
1327 	if ((error = nlookup(nd)) != 0)
1328 		return (error);
1329 	mp = nd->nl_nch.mount;
1330 	sp = &mp->mnt_stat;
1331 
1332 	/*
1333 	 * Ignore refresh error, user should have visibility.
1334 	 * This can happen if a NFS mount goes bad (e.g. server
1335 	 * revokes perms or goes down).
1336 	 */
1337 	error = VFS_STATFS(mp, sp, nd->nl_cred);
1338 	/* ignore error */
1339 
1340 	error = mount_path(p, mp, &fullpath, &freepath);
1341 	if (error)
1342 		return(error);
1343 	bzero(sp->f_mntonname, sizeof(sp->f_mntonname));
1344 	strlcpy(sp->f_mntonname, fullpath, sizeof(sp->f_mntonname));
1345 	kfree(freepath, M_TEMP);
1346 
1347 	sp->f_flags = mp->mnt_flag & MNT_VISFLAGMASK;
1348 	bcopy(sp, buf, sizeof(*buf));
1349 	/* Only root should have access to the fsid's. */
1350 	if (priv_check(td, PRIV_ROOT))
1351 		buf->f_fsid.val[0] = buf->f_fsid.val[1] = 0;
1352 	return (0);
1353 }
1354 
1355 /*
1356  * statfs_args(char *path, struct statfs *buf)
1357  *
1358  * Get filesystem statistics.
1359  */
1360 int
1361 sys_statfs(struct sysmsg *sysmsg, const struct statfs_args *uap)
1362 {
1363 	struct nlookupdata nd;
1364 	struct statfs buf;
1365 	int error;
1366 
1367 	error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
1368 	if (error == 0)
1369 		error = kern_statfs(&nd, &buf);
1370 	nlookup_done(&nd);
1371 	if (error == 0)
1372 		error = copyout(&buf, uap->buf, sizeof(*uap->buf));
1373 	return (error);
1374 }
1375 
1376 int
1377 kern_fstatfs(int fd, struct statfs *buf)
1378 {
1379 	struct thread *td = curthread;
1380 	struct proc *p = td->td_proc;
1381 	struct file *fp;
1382 	struct mount *mp;
1383 	struct statfs *sp;
1384 	char *fullpath, *freepath;
1385 	int error;
1386 
1387 	KKASSERT(p);
1388 	if ((error = holdvnode(td, fd, &fp)) != 0)
1389 		return (error);
1390 
1391 	/*
1392 	 * Try to use mount info from any overlays rather than the
1393 	 * mount info for the underlying vnode, otherwise we will
1394 	 * fail when operating on null-mounted paths inside a chroot.
1395 	 */
1396 	if ((mp = fp->f_nchandle.mount) == NULL)
1397 		mp = ((struct vnode *)fp->f_data)->v_mount;
1398 	if (mp == NULL) {
1399 		error = EBADF;
1400 		goto done;
1401 	}
1402 	if (fp->f_cred == NULL) {
1403 		error = EINVAL;
1404 		goto done;
1405 	}
1406 
1407 	/*
1408 	 * Ignore refresh error, user should have visibility.
1409 	 * This can happen if a NFS mount goes bad (e.g. server
1410 	 * revokes perms or goes down).
1411 	 */
1412 	sp = &mp->mnt_stat;
1413 	error = VFS_STATFS(mp, sp, fp->f_cred);
1414 
1415 	if ((error = mount_path(p, mp, &fullpath, &freepath)) != 0)
1416 		goto done;
1417 	bzero(sp->f_mntonname, sizeof(sp->f_mntonname));
1418 	strlcpy(sp->f_mntonname, fullpath, sizeof(sp->f_mntonname));
1419 	kfree(freepath, M_TEMP);
1420 
1421 	sp->f_flags = mp->mnt_flag & MNT_VISFLAGMASK;
1422 	bcopy(sp, buf, sizeof(*buf));
1423 
1424 	/* Only root should have access to the fsid's. */
1425 	if (priv_check(td, PRIV_ROOT))
1426 		buf->f_fsid.val[0] = buf->f_fsid.val[1] = 0;
1427 	error = 0;
1428 done:
1429 	fdrop(fp);
1430 	return (error);
1431 }
1432 
1433 /*
1434  * fstatfs_args(int fd, struct statfs *buf)
1435  *
1436  * Get filesystem statistics.
1437  */
1438 int
1439 sys_fstatfs(struct sysmsg *sysmsg, const struct fstatfs_args *uap)
1440 {
1441 	struct statfs buf;
1442 	int error;
1443 
1444 	error = kern_fstatfs(uap->fd, &buf);
1445 
1446 	if (error == 0)
1447 		error = copyout(&buf, uap->buf, sizeof(*uap->buf));
1448 	return (error);
1449 }
1450 
1451 int
1452 kern_statvfs(struct nlookupdata *nd, struct statvfs *buf)
1453 {
1454 	struct mount *mp;
1455 	struct statvfs *sp;
1456 	int error;
1457 
1458 	if ((error = nlookup(nd)) != 0)
1459 		return (error);
1460 	mp = nd->nl_nch.mount;
1461 	sp = &mp->mnt_vstat;
1462 	if ((error = VFS_STATVFS(mp, sp, nd->nl_cred)) != 0)
1463 		return (error);
1464 
1465 	sp->f_flag = 0;
1466 	if (mp->mnt_flag & MNT_RDONLY)
1467 		sp->f_flag |= ST_RDONLY;
1468 	if (mp->mnt_flag & MNT_NOSUID)
1469 		sp->f_flag |= ST_NOSUID;
1470 	bcopy(sp, buf, sizeof(*buf));
1471 	return (0);
1472 }
1473 
1474 /*
1475  * statfs_args(char *path, struct statfs *buf)
1476  *
1477  * Get filesystem statistics.
1478  */
1479 int
1480 sys_statvfs(struct sysmsg *sysmsg, const struct statvfs_args *uap)
1481 {
1482 	struct nlookupdata nd;
1483 	struct statvfs buf;
1484 	int error;
1485 
1486 	error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
1487 	if (error == 0)
1488 		error = kern_statvfs(&nd, &buf);
1489 	nlookup_done(&nd);
1490 	if (error == 0)
1491 		error = copyout(&buf, uap->buf, sizeof(*uap->buf));
1492 	return (error);
1493 }
1494 
1495 int
1496 kern_fstatvfs(int fd, struct statvfs *buf)
1497 {
1498 	struct thread *td = curthread;
1499 	struct file *fp;
1500 	struct mount *mp;
1501 	struct statvfs *sp;
1502 	int error;
1503 
1504 	if ((error = holdvnode(td, fd, &fp)) != 0)
1505 		return (error);
1506 	if ((mp = fp->f_nchandle.mount) == NULL)
1507 		mp = ((struct vnode *)fp->f_data)->v_mount;
1508 	if (mp == NULL) {
1509 		error = EBADF;
1510 		goto done;
1511 	}
1512 	if (fp->f_cred == NULL) {
1513 		error = EINVAL;
1514 		goto done;
1515 	}
1516 	sp = &mp->mnt_vstat;
1517 	if ((error = VFS_STATVFS(mp, sp, fp->f_cred)) != 0)
1518 		goto done;
1519 
1520 	sp->f_flag = 0;
1521 	if (mp->mnt_flag & MNT_RDONLY)
1522 		sp->f_flag |= ST_RDONLY;
1523 	if (mp->mnt_flag & MNT_NOSUID)
1524 		sp->f_flag |= ST_NOSUID;
1525 
1526 	bcopy(sp, buf, sizeof(*buf));
1527 	error = 0;
1528 done:
1529 	fdrop(fp);
1530 	return (error);
1531 }
1532 
1533 /*
1534  * fstatfs_args(int fd, struct statfs *buf)
1535  *
1536  * Get filesystem statistics.
1537  */
1538 int
1539 sys_fstatvfs(struct sysmsg *sysmsg, const struct fstatvfs_args *uap)
1540 {
1541 	struct statvfs buf;
1542 	int error;
1543 
1544 	error = kern_fstatvfs(uap->fd, &buf);
1545 
1546 	if (error == 0)
1547 		error = copyout(&buf, uap->buf, sizeof(*uap->buf));
1548 	return (error);
1549 }
1550 
1551 /*
1552  * getfsstat_args(struct statfs *buf, long bufsize, int flags)
1553  *
1554  * Get statistics on all filesystems.
1555  */
1556 
1557 struct getfsstat_info {
1558 	struct statfs *sfsp;
1559 	long count;
1560 	long maxcount;
1561 	int error;
1562 	int flags;
1563 	struct thread *td;
1564 };
1565 
1566 static int getfsstat_callback(struct mount *, void *);
1567 
1568 int
1569 sys_getfsstat(struct sysmsg *sysmsg, const struct getfsstat_args *uap)
1570 {
1571 	struct thread *td = curthread;
1572 	struct getfsstat_info info;
1573 
1574 	bzero(&info, sizeof(info));
1575 
1576 	info.maxcount = uap->bufsize / sizeof(struct statfs);
1577 	info.sfsp = uap->buf;
1578 	info.count = 0;
1579 	info.flags = uap->flags;
1580 	info.td = td;
1581 
1582 	mountlist_scan(getfsstat_callback, &info, MNTSCAN_FORWARD);
1583 	if (info.sfsp && info.count > info.maxcount)
1584 		sysmsg->sysmsg_result = info.maxcount;
1585 	else
1586 		sysmsg->sysmsg_result = info.count;
1587 	return (info.error);
1588 }
1589 
1590 static int
1591 getfsstat_callback(struct mount *mp, void *data)
1592 {
1593 	struct getfsstat_info *info = data;
1594 	struct statfs *sp;
1595 	char *freepath;
1596 	char *fullpath;
1597 	int error;
1598 
1599 	if (info->td->td_proc && !chroot_visible_mnt(mp, info->td->td_proc))
1600 		return(0);
1601 
1602 	if (info->sfsp && info->count < info->maxcount) {
1603 		sp = &mp->mnt_stat;
1604 
1605 		/*
1606 		 * If MNT_NOWAIT or MNT_LAZY is specified, do not
1607 		 * refresh the fsstat cache. MNT_NOWAIT or MNT_LAZY
1608 		 * overrides MNT_WAIT.
1609 		 *
1610 		 * Ignore refresh error, user should have visibility.
1611 		 * This can happen if a NFS mount goes bad (e.g. server
1612 		 * revokes perms or goes down).
1613 		 */
1614 		if (((info->flags & (MNT_LAZY|MNT_NOWAIT)) == 0 ||
1615 		    (info->flags & MNT_WAIT)) &&
1616 		    (error = VFS_STATFS(mp, sp, info->td->td_ucred))) {
1617 			/* ignore error */
1618 		}
1619 		sp->f_flags = mp->mnt_flag & MNT_VISFLAGMASK;
1620 
1621 		error = mount_path(info->td->td_proc, mp, &fullpath, &freepath);
1622 		if (error) {
1623 			info->error = error;
1624 			return(-1);
1625 		}
1626 		bzero(sp->f_mntonname, sizeof(sp->f_mntonname));
1627 		strlcpy(sp->f_mntonname, fullpath, sizeof(sp->f_mntonname));
1628 		kfree(freepath, M_TEMP);
1629 
1630 		error = copyout(sp, info->sfsp, sizeof(*sp));
1631 		if (error) {
1632 			info->error = error;
1633 			return (-1);
1634 		}
1635 		++info->sfsp;
1636 	}
1637 	info->count++;
1638 	return(0);
1639 }
1640 
1641 /*
1642  * getvfsstat_args(struct statfs *buf, struct statvfs *vbuf,
1643 		   long bufsize, int flags)
1644  *
1645  * Get statistics on all filesystems.
1646  */
1647 
1648 struct getvfsstat_info {
1649 	struct statfs *sfsp;
1650 	struct statvfs *vsfsp;
1651 	long count;
1652 	long maxcount;
1653 	int error;
1654 	int flags;
1655 	struct thread *td;
1656 };
1657 
1658 static int getvfsstat_callback(struct mount *, void *);
1659 
1660 int
1661 sys_getvfsstat(struct sysmsg *sysmsg, const struct getvfsstat_args *uap)
1662 {
1663 	struct thread *td = curthread;
1664 	struct getvfsstat_info info;
1665 
1666 	bzero(&info, sizeof(info));
1667 
1668 	info.maxcount = uap->vbufsize / sizeof(struct statvfs);
1669 	info.sfsp = uap->buf;
1670 	info.vsfsp = uap->vbuf;
1671 	info.count = 0;
1672 	info.flags = uap->flags;
1673 	info.td = td;
1674 
1675 	mountlist_scan(getvfsstat_callback, &info, MNTSCAN_FORWARD);
1676 	if (info.vsfsp && info.count > info.maxcount)
1677 		sysmsg->sysmsg_result = info.maxcount;
1678 	else
1679 		sysmsg->sysmsg_result = info.count;
1680 	return (info.error);
1681 }
1682 
1683 static int
1684 getvfsstat_callback(struct mount *mp, void *data)
1685 {
1686 	struct getvfsstat_info *info = data;
1687 	struct statfs *sp;
1688 	struct statvfs *vsp;
1689 	char *freepath;
1690 	char *fullpath;
1691 	int error;
1692 
1693 	if (info->td->td_proc && !chroot_visible_mnt(mp, info->td->td_proc))
1694 		return(0);
1695 
1696 	if (info->vsfsp && info->count < info->maxcount) {
1697 		sp = &mp->mnt_stat;
1698 		vsp = &mp->mnt_vstat;
1699 
1700 		/*
1701 		 * If MNT_NOWAIT or MNT_LAZY is specified, do not
1702 		 * refresh the fsstat cache. MNT_NOWAIT or MNT_LAZY
1703 		 * overrides MNT_WAIT.
1704 		 *
1705 		 * Ignore refresh error, user should have visibility.
1706 		 * This can happen if a NFS mount goes bad (e.g. server
1707 		 * revokes perms or goes down).
1708 		 */
1709 		if (((info->flags & (MNT_LAZY|MNT_NOWAIT)) == 0 ||
1710 		    (info->flags & MNT_WAIT)) &&
1711 		    (error = VFS_STATFS(mp, sp, info->td->td_ucred))) {
1712 			/* ignore error */
1713 		}
1714 		sp->f_flags = mp->mnt_flag & MNT_VISFLAGMASK;
1715 
1716 		if (((info->flags & (MNT_LAZY|MNT_NOWAIT)) == 0 ||
1717 		    (info->flags & MNT_WAIT)) &&
1718 		    (error = VFS_STATVFS(mp, vsp, info->td->td_ucred))) {
1719 			/* ignore error */
1720 		}
1721 		vsp->f_flag = 0;
1722 		if (mp->mnt_flag & MNT_RDONLY)
1723 			vsp->f_flag |= ST_RDONLY;
1724 		if (mp->mnt_flag & MNT_NOSUID)
1725 			vsp->f_flag |= ST_NOSUID;
1726 
1727 		error = mount_path(info->td->td_proc, mp, &fullpath, &freepath);
1728 		if (error) {
1729 			info->error = error;
1730 			return(-1);
1731 		}
1732 		bzero(sp->f_mntonname, sizeof(sp->f_mntonname));
1733 		strlcpy(sp->f_mntonname, fullpath, sizeof(sp->f_mntonname));
1734 		kfree(freepath, M_TEMP);
1735 
1736 		error = copyout(sp, info->sfsp, sizeof(*sp));
1737 		if (error == 0)
1738 			error = copyout(vsp, info->vsfsp, sizeof(*vsp));
1739 		if (error) {
1740 			info->error = error;
1741 			return (-1);
1742 		}
1743 		++info->sfsp;
1744 		++info->vsfsp;
1745 	}
1746 	info->count++;
1747 	return(0);
1748 }
1749 
1750 
1751 /*
1752  * fchdir_args(int fd)
1753  *
1754  * Change current working directory to a given file descriptor.
1755  */
1756 int
1757 sys_fchdir(struct sysmsg *sysmsg, const struct fchdir_args *uap)
1758 {
1759 	struct thread *td = curthread;
1760 	struct proc *p = td->td_proc;
1761 	struct filedesc *fdp = p->p_fd;
1762 	struct vnode *vp, *ovp;
1763 	struct mount *mp;
1764 	struct file *fp;
1765 	struct nchandle nch, onch, tnch;
1766 	int error;
1767 
1768 	if ((error = holdvnode(td, uap->fd, &fp)) != 0)
1769 		return (error);
1770 	lwkt_gettoken(&p->p_token);
1771 	vp = (struct vnode *)fp->f_data;
1772 	vref(vp);
1773 	vn_lock(vp, LK_SHARED | LK_RETRY);
1774 	if (fp->f_nchandle.ncp == NULL)
1775 		error = ENOTDIR;
1776 	else
1777 		error = checkvp_chdir(vp, td);
1778 	if (error) {
1779 		vput(vp);
1780 		goto done;
1781 	}
1782 	cache_copy(&fp->f_nchandle, &nch);
1783 
1784 	/*
1785 	 * If the ncp has become a mount point, traverse through
1786 	 * the mount point.
1787 	 */
1788 
1789 	while (!error && (nch.ncp->nc_flag & NCF_ISMOUNTPT) &&
1790 	       (mp = cache_findmount(&nch)) != NULL
1791 	) {
1792 		error = nlookup_mp(mp, &tnch);
1793 		if (error == 0) {
1794 			cache_unlock(&tnch);	/* leave ref intact */
1795 			vput(vp);
1796 			vp = tnch.ncp->nc_vp;
1797 			error = vget(vp, LK_SHARED);
1798 			KKASSERT(error == 0);
1799 			cache_drop(&nch);
1800 			nch = tnch;
1801 		}
1802 		cache_dropmount(mp);
1803 	}
1804 	if (error == 0) {
1805 		spin_lock(&fdp->fd_spin);
1806 		ovp = fdp->fd_cdir;
1807 		onch = fdp->fd_ncdir;
1808 		fdp->fd_cdir = vp;
1809 		fdp->fd_ncdir = nch;
1810 		spin_unlock(&fdp->fd_spin);
1811 		vn_unlock(vp);		/* leave ref intact */
1812 		cache_drop(&onch);
1813 		vrele(ovp);
1814 	} else {
1815 		cache_drop(&nch);
1816 		vput(vp);
1817 	}
1818 	fdrop(fp);
1819 done:
1820 	lwkt_reltoken(&p->p_token);
1821 	return (error);
1822 }
1823 
1824 int
1825 kern_chdir(struct nlookupdata *nd)
1826 {
1827 	struct thread *td = curthread;
1828 	struct proc *p = td->td_proc;
1829 	struct filedesc *fdp = p->p_fd;
1830 	struct vnode *vp, *ovp;
1831 	struct nchandle onch;
1832 	int error;
1833 
1834 	nd->nl_flags |= NLC_SHAREDLOCK;
1835 	if ((error = nlookup(nd)) != 0)
1836 		return (error);
1837 	if ((vp = nd->nl_nch.ncp->nc_vp) == NULL)
1838 		return (ENOENT);
1839 	if ((error = vget(vp, LK_SHARED)) != 0)
1840 		return (error);
1841 
1842 	lwkt_gettoken(&p->p_token);
1843 	error = checkvp_chdir(vp, td);
1844 	vn_unlock(vp);
1845 	if (error == 0) {
1846 		spin_lock(&fdp->fd_spin);
1847 		ovp = fdp->fd_cdir;
1848 		onch = fdp->fd_ncdir;
1849 		fdp->fd_ncdir = nd->nl_nch;
1850 		fdp->fd_cdir = vp;
1851 		spin_unlock(&fdp->fd_spin);
1852 		cache_unlock(&nd->nl_nch);	/* leave reference intact */
1853 		cache_drop(&onch);
1854 		vrele(ovp);
1855 		cache_zero(&nd->nl_nch);
1856 	} else {
1857 		vrele(vp);
1858 	}
1859 	lwkt_reltoken(&p->p_token);
1860 	return (error);
1861 }
1862 
1863 /*
1864  * chdir_args(char *path)
1865  *
1866  * Change current working directory (``.'').
1867  */
1868 int
1869 sys_chdir(struct sysmsg *sysmsg, const struct chdir_args *uap)
1870 {
1871 	struct nlookupdata nd;
1872 	int error;
1873 
1874 	error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
1875 	if (error == 0)
1876 		error = kern_chdir(&nd);
1877 	nlookup_done(&nd);
1878 	return (error);
1879 }
1880 
1881 /*
1882  * Helper function for raised chroot(2) security function:  Refuse if
1883  * any filedescriptors are open directories.
1884  */
1885 static int
1886 chroot_refuse_vdir_fds(thread_t td, struct filedesc *fdp)
1887 {
1888 	struct vnode *vp;
1889 	struct file *fp;
1890 	int error;
1891 	int fd;
1892 
1893 	for (fd = 0; fd < fdp->fd_nfiles ; fd++) {
1894 		if ((error = holdvnode(td, fd, &fp)) != 0)
1895 			continue;
1896 		vp = (struct vnode *)fp->f_data;
1897 		if (vp->v_type != VDIR) {
1898 			fdrop(fp);
1899 			continue;
1900 		}
1901 		fdrop(fp);
1902 		return(EPERM);
1903 	}
1904 	return (0);
1905 }
1906 
1907 /*
1908  * This sysctl determines if we will allow a process to chroot(2) if it
1909  * has a directory open:
1910  *	0: disallowed for all processes.
1911  *	1: allowed for processes that were not already chroot(2)'ed.
1912  *	2: allowed for all processes.
1913  */
1914 
1915 static int chroot_allow_open_directories = 1;
1916 
1917 SYSCTL_INT(_kern, OID_AUTO, chroot_allow_open_directories, CTLFLAG_RW,
1918      &chroot_allow_open_directories, 0, "");
1919 
1920 /*
1921  * chroot to the specified namecache entry.  We obtain the vp from the
1922  * namecache data.  The passed ncp must be locked and referenced and will
1923  * remain locked and referenced on return.
1924  */
1925 int
1926 kern_chroot(struct nchandle *nch)
1927 {
1928 	struct thread *td = curthread;
1929 	struct proc *p = td->td_proc;
1930 	struct filedesc *fdp = p->p_fd;
1931 	struct vnode *vp;
1932 	int error;
1933 
1934 	/*
1935 	 * Only privileged user can chroot
1936 	 */
1937 	error = priv_check_cred(td->td_ucred, PRIV_VFS_CHROOT, 0);
1938 	if (error)
1939 		return (error);
1940 
1941 	/*
1942 	 * Disallow open directory descriptors (fchdir() breakouts).
1943 	 */
1944 	if (chroot_allow_open_directories == 0 ||
1945 	   (chroot_allow_open_directories == 1 && fdp->fd_rdir != rootvnode)) {
1946 		if ((error = chroot_refuse_vdir_fds(td, fdp)) != 0)
1947 			return (error);
1948 	}
1949 	if ((vp = nch->ncp->nc_vp) == NULL)
1950 		return (ENOENT);
1951 
1952 	if ((error = vget(vp, LK_SHARED)) != 0)
1953 		return (error);
1954 
1955 	/*
1956 	 * Check the validity of vp as a directory to change to and
1957 	 * associate it with rdir/jdir.
1958 	 */
1959 	error = checkvp_chdir(vp, td);
1960 	vn_unlock(vp);			/* leave reference intact */
1961 	if (error == 0) {
1962 		lwkt_gettoken(&p->p_token);
1963 		vrele(fdp->fd_rdir);
1964 		fdp->fd_rdir = vp;	/* reference inherited by fd_rdir */
1965 		cache_drop(&fdp->fd_nrdir);
1966 		cache_copy(nch, &fdp->fd_nrdir);
1967 		if (fdp->fd_jdir == NULL) {
1968 			fdp->fd_jdir = vp;
1969 			vref(fdp->fd_jdir);
1970 			cache_copy(nch, &fdp->fd_njdir);
1971 		}
1972 		if ((p->p_flags & P_DIDCHROOT) == 0) {
1973 			p->p_flags |= P_DIDCHROOT;
1974 			if (p->p_depth <= 65535 - 32)
1975 				p->p_depth += 32;
1976 		}
1977 		lwkt_reltoken(&p->p_token);
1978 	} else {
1979 		vrele(vp);
1980 	}
1981 	return (error);
1982 }
1983 
1984 /*
1985  * chroot_args(char *path)
1986  *
1987  * Change notion of root (``/'') directory.
1988  */
1989 int
1990 sys_chroot(struct sysmsg *sysmsg, const struct chroot_args *uap)
1991 {
1992 	struct thread *td __debugvar = curthread;
1993 	struct nlookupdata nd;
1994 	int error;
1995 
1996 	KKASSERT(td->td_proc);
1997 	error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
1998 	if (error == 0) {
1999 		nd.nl_flags |= NLC_EXEC;
2000 		error = nlookup(&nd);
2001 		if (error == 0)
2002 			error = kern_chroot(&nd.nl_nch);
2003 	}
2004 	nlookup_done(&nd);
2005 	return(error);
2006 }
2007 
2008 int
2009 sys_chroot_kernel(struct sysmsg *sysmsg, const struct chroot_kernel_args *uap)
2010 {
2011 	struct thread *td = curthread;
2012 	struct nlookupdata nd;
2013 	struct nchandle *nch;
2014 	struct vnode *vp;
2015 	int error;
2016 
2017 	error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
2018 	if (error)
2019 		goto error_nond;
2020 
2021 	error = nlookup(&nd);
2022 	if (error)
2023 		goto error_out;
2024 
2025 	nch = &nd.nl_nch;
2026 
2027 	error = priv_check_cred(td->td_ucred, PRIV_VFS_CHROOT, 0);
2028 	if (error)
2029 		goto error_out;
2030 
2031 	if ((vp = nch->ncp->nc_vp) == NULL) {
2032 		error = ENOENT;
2033 		goto error_out;
2034 	}
2035 
2036 	if ((error = cache_vref(nch, nd.nl_cred, &vp)) != 0)
2037 		goto error_out;
2038 
2039 	vfs_cache_setroot(vp, cache_hold(nch));
2040 
2041 error_out:
2042 	nlookup_done(&nd);
2043 error_nond:
2044 	return(error);
2045 }
2046 
2047 /*
2048  * Common routine for chroot and chdir.  Given a locked, referenced vnode,
2049  * determine whether it is legal to chdir to the vnode.  The vnode's state
2050  * is not changed by this call.
2051  */
2052 static int
2053 checkvp_chdir(struct vnode *vp, struct thread *td)
2054 {
2055 	int error;
2056 
2057 	if (vp->v_type != VDIR)
2058 		error = ENOTDIR;
2059 	else
2060 		error = VOP_EACCESS(vp, VEXEC, td->td_ucred);
2061 	return (error);
2062 }
2063 
2064 int
2065 kern_open(struct nlookupdata *nd, int oflags, int mode, int *res)
2066 {
2067 	struct thread *td = curthread;
2068 	struct proc *p = td->td_proc;
2069 	struct lwp *lp = td->td_lwp;
2070 	struct filedesc *fdp = p->p_fd;
2071 	int cmode, flags;
2072 	struct file *nfp;
2073 	struct file *fp;
2074 	int type, indx, error = 0;
2075 	struct flock lf;
2076 
2077 	if ((oflags & O_ACCMODE) == O_ACCMODE)
2078 		return (EINVAL);
2079 	flags = FFLAGS(oflags);
2080 	error = falloc(lp, &nfp, NULL);
2081 	if (error)
2082 		return (error);
2083 	fp = nfp;
2084 	cmode = ((mode &~ fdp->fd_cmask) & ALLPERMS) & ~S_ISTXT;
2085 
2086 	/*
2087 	 * Call vn_open() to do the lookup and assign the vnode to the
2088 	 * file pointer.  vn_open() does not change the ref count on fp
2089 	 * and the vnode, on success, will be inherited by the file pointer
2090 	 * and unlocked.
2091 	 *
2092 	 * Request a shared lock on the vnode if possible.
2093 	 *
2094 	 * When NLC_SHAREDLOCK is set we may still need an exclusive vnode
2095 	 * lock for O_RDWR opens on executables in order to avoid a VTEXT
2096 	 * detection race.  The NLC_EXCLLOCK_IFEXEC handles this case.
2097 	 *
2098 	 * NOTE: We need a flag to separate terminal vnode locking from
2099 	 *	 parent locking.  O_CREAT needs parent locking, but O_TRUNC
2100 	 *	 and O_RDWR only need to lock the terminal vnode exclusively.
2101 	 */
2102 	nd->nl_flags |= NLC_LOCKVP;
2103 	if ((flags & (O_CREAT|O_TRUNC)) == 0) {
2104 		nd->nl_flags |= NLC_SHAREDLOCK;
2105 		if (flags & O_RDWR)
2106 			nd->nl_flags |= NLC_EXCLLOCK_IFEXEC;
2107 	}
2108 
2109 	/*
2110 	 * Issue the vn_open, passing in the referenced fp.  the vn_open()
2111 	 * is allowed to replace fp by fdrop()ing it and returning its own
2112 	 * referenced fp.
2113 	 */
2114 	nfp = fp;
2115 	error = vn_open(nd, &nfp, flags, cmode);
2116 	fp = nfp;
2117 	nlookup_done(nd);
2118 
2119 	/*
2120 	 * Deal with any error condition
2121 	 */
2122 	if (error) {
2123 		fdrop(fp);	/* our ref */
2124 		if (error == ERESTART)
2125 			error = EINTR;
2126 		return (error);
2127 	}
2128 
2129 	/*
2130 	 * Reserve a file descriptor.
2131 	 */
2132 	if ((error = fdalloc(p, 0, &indx)) != 0) {
2133 		fdrop(fp);
2134 		return (error);
2135 	}
2136 
2137 	/*
2138 	 * Handle advisory lock flags.  This is only supported with vnodes.
2139 	 * For things like /dev/fd/N we might not actually get a vnode.
2140 	 */
2141 	if ((flags & (O_EXLOCK | O_SHLOCK)) && fp->f_type == DTYPE_VNODE) {
2142 		struct vnode *vp;
2143 
2144 		vp = (struct vnode *)fp->f_data;
2145 		vref(vp);
2146 
2147 		lf.l_whence = SEEK_SET;
2148 		lf.l_start = 0;
2149 		lf.l_len = 0;
2150 		if (flags & O_EXLOCK)
2151 			lf.l_type = F_WRLCK;
2152 		else
2153 			lf.l_type = F_RDLCK;
2154 		if (flags & FNONBLOCK)
2155 			type = 0;
2156 		else
2157 			type = F_WAIT;
2158 
2159 		error = VOP_ADVLOCK(vp, (caddr_t)fp, F_SETLK, &lf, type);
2160 		if (error) {
2161 			/*
2162 			 * lock request failed.  Clean up the reserved
2163 			 * descriptor.
2164 			 */
2165 			vrele(vp);
2166 			fsetfd(fdp, NULL, indx);
2167 			fdrop(fp);
2168 			return (error);
2169 		}
2170 		atomic_set_int(&fp->f_flag, FHASLOCK); /* race ok */
2171 		vrele(vp);
2172 	}
2173 
2174 	/*
2175 	 * release our private reference, leaving the one associated with the
2176 	 * descriptor table intact.
2177 	 */
2178 	if (oflags & O_CLOEXEC)
2179 		fdp->fd_files[indx].fileflags |= UF_EXCLOSE;
2180 	fsetfd(fdp, fp, indx);
2181 	fdrop(fp);
2182 	*res = indx;
2183 
2184 	return (error);
2185 }
2186 
2187 /*
2188  * open_args(char *path, int flags, int mode)
2189  *
2190  * Check permissions, allocate an open file structure,
2191  * and call the device open routine if any.
2192  */
2193 int
2194 sys_open(struct sysmsg *sysmsg, const struct open_args *uap)
2195 {
2196 	struct nlookupdata nd;
2197 	int error;
2198 
2199 	error = nlookup_init(&nd, uap->path, UIO_USERSPACE, 0);
2200 	if (error == 0) {
2201 		error = kern_open(&nd, uap->flags,
2202 				    uap->mode, &sysmsg->sysmsg_result);
2203 	}
2204 	nlookup_done(&nd);
2205 	return (error);
2206 }
2207 
2208 /*
2209  * openat_args(int fd, char *path, int flags, int mode)
2210  */
2211 int
2212 sys_openat(struct sysmsg *sysmsg, const struct openat_args *uap)
2213 {
2214 	struct nlookupdata nd;
2215 	int error;
2216 	struct file *fp;
2217 
2218 	error = nlookup_init_at(&nd, &fp, uap->fd, uap->path, UIO_USERSPACE, 0);
2219 	if (error == 0) {
2220 		error = kern_open(&nd, uap->flags, uap->mode,
2221 					&sysmsg->sysmsg_result);
2222 	}
2223 	nlookup_done_at(&nd, fp);
2224 	return (error);
2225 }
2226 
2227 int
2228 kern_mknod(struct nlookupdata *nd, int mode, int rmajor, int rminor)
2229 {
2230 	struct thread *td = curthread;
2231 	struct proc *p = td->td_proc;
2232 	struct vnode *vp;
2233 	struct vattr vattr;
2234 	int error;
2235 	int whiteout = 0;
2236 
2237 	KKASSERT(p);
2238 
2239 	VATTR_NULL(&vattr);
2240 	vattr.va_mode = (mode & ALLPERMS) &~ p->p_fd->fd_cmask;
2241 	vattr.va_rmajor = rmajor;
2242 	vattr.va_rminor = rminor;
2243 
2244 	switch (mode & S_IFMT) {
2245 	case S_IFMT:	/* used by badsect to flag bad sectors */
2246 		error = priv_check_cred(td->td_ucred, PRIV_VFS_MKNOD_BAD, 0);
2247 		vattr.va_type = VBAD;
2248 		break;
2249 	case S_IFCHR:
2250 		error = priv_check(td, PRIV_VFS_MKNOD_DEV);
2251 		vattr.va_type = VCHR;
2252 		break;
2253 	case S_IFBLK:
2254 		error = priv_check(td, PRIV_VFS_MKNOD_DEV);
2255 		vattr.va_type = VBLK;
2256 		break;
2257 	case S_IFWHT:
2258 		error = priv_check_cred(td->td_ucred, PRIV_VFS_MKNOD_WHT, 0);
2259 		whiteout = 1;
2260 		break;
2261 	case S_IFDIR:	/* special directories support for HAMMER */
2262 		error = priv_check_cred(td->td_ucred, PRIV_VFS_MKNOD_DIR, 0);
2263 		vattr.va_type = VDIR;
2264 		break;
2265 	case S_IFIFO:
2266 		return (kern_mkfifo(nd, mode));
2267 		break;
2268 	default:
2269 		error = EINVAL;
2270 		break;
2271 	}
2272 
2273 	if (error)
2274 		return (error);
2275 
2276 	bwillinode(1);
2277 	nd->nl_flags |= NLC_CREATE | NLC_REFDVP;
2278 	if ((error = nlookup(nd)) != 0)
2279 		return (error);
2280 	if (nd->nl_nch.ncp->nc_vp)
2281 		return (EEXIST);
2282 	if (nd->nl_dvp == NULL)
2283 		return (EINVAL);
2284 	if ((error = ncp_writechk(&nd->nl_nch)) != 0)
2285 		return (error);
2286 
2287 	if (whiteout) {
2288 		error = VOP_NWHITEOUT(&nd->nl_nch, nd->nl_dvp,
2289 				      nd->nl_cred, NAMEI_CREATE);
2290 	} else {
2291 		vp = NULL;
2292 		error = VOP_NMKNOD(&nd->nl_nch, nd->nl_dvp,
2293 				   &vp, nd->nl_cred, &vattr);
2294 		if (error == 0)
2295 			vput(vp);
2296 	}
2297 	return (error);
2298 }
2299 
2300 /*
2301  * mknod_args(char *path, int mode, int dev)
2302  *
2303  * Create a special file.
2304  */
2305 int
2306 sys_mknod(struct sysmsg *sysmsg, const struct mknod_args *uap)
2307 {
2308 	struct nlookupdata nd;
2309 	int error;
2310 
2311 	error = nlookup_init(&nd, uap->path, UIO_USERSPACE, 0);
2312 	if (error == 0) {
2313 		error = kern_mknod(&nd, uap->mode,
2314 				   umajor(uap->dev), uminor(uap->dev));
2315 	}
2316 	nlookup_done(&nd);
2317 	return (error);
2318 }
2319 
2320 /*
2321  * mknodat_args(int fd, char *path, mode_t mode, dev_t dev)
2322  *
2323  * Create a special file.  The path is relative to the directory associated
2324  * with fd.
2325  */
2326 int
2327 sys_mknodat(struct sysmsg *sysmsg, const struct mknodat_args *uap)
2328 {
2329 	struct nlookupdata nd;
2330 	struct file *fp;
2331 	int error;
2332 
2333 	error = nlookup_init_at(&nd, &fp, uap->fd, uap->path, UIO_USERSPACE, 0);
2334 	if (error == 0) {
2335 		error = kern_mknod(&nd, uap->mode,
2336 				   umajor(uap->dev), uminor(uap->dev));
2337 	}
2338 	nlookup_done_at(&nd, fp);
2339 	return (error);
2340 }
2341 
2342 int
2343 kern_mkfifo(struct nlookupdata *nd, int mode)
2344 {
2345 	struct thread *td = curthread;
2346 	struct proc *p = td->td_proc;
2347 	struct vattr vattr;
2348 	struct vnode *vp;
2349 	int error;
2350 
2351 	bwillinode(1);
2352 
2353 	nd->nl_flags |= NLC_CREATE | NLC_REFDVP;
2354 	if ((error = nlookup(nd)) != 0)
2355 		return (error);
2356 	if (nd->nl_nch.ncp->nc_vp)
2357 		return (EEXIST);
2358 	if (nd->nl_dvp == NULL)
2359 		return (EINVAL);
2360 	if ((error = ncp_writechk(&nd->nl_nch)) != 0)
2361 		return (error);
2362 
2363 	VATTR_NULL(&vattr);
2364 	vattr.va_type = VFIFO;
2365 	vattr.va_mode = (mode & ALLPERMS) &~ p->p_fd->fd_cmask;
2366 	vp = NULL;
2367 	error = VOP_NMKNOD(&nd->nl_nch, nd->nl_dvp, &vp, nd->nl_cred, &vattr);
2368 	if (error == 0)
2369 		vput(vp);
2370 	return (error);
2371 }
2372 
2373 /*
2374  * mkfifo_args(char *path, int mode)
2375  *
2376  * Create a named pipe.
2377  */
2378 int
2379 sys_mkfifo(struct sysmsg *sysmsg, const struct mkfifo_args *uap)
2380 {
2381 	struct nlookupdata nd;
2382 	int error;
2383 
2384 	error = nlookup_init(&nd, uap->path, UIO_USERSPACE, 0);
2385 	if (error == 0)
2386 		error = kern_mkfifo(&nd, uap->mode);
2387 	nlookup_done(&nd);
2388 	return (error);
2389 }
2390 
2391 /*
2392  * mkfifoat_args(int fd, char *path, mode_t mode)
2393  *
2394  * Create a named pipe.  The path is relative to the directory associated
2395  * with fd.
2396  */
2397 int
2398 sys_mkfifoat(struct sysmsg *sysmsg, const struct mkfifoat_args *uap)
2399 {
2400 	struct nlookupdata nd;
2401 	struct file *fp;
2402 	int error;
2403 
2404 	error = nlookup_init_at(&nd, &fp, uap->fd, uap->path, UIO_USERSPACE, 0);
2405 	if (error == 0)
2406 		error = kern_mkfifo(&nd, uap->mode);
2407 	nlookup_done_at(&nd, fp);
2408 	return (error);
2409 }
2410 
2411 static int hardlink_check_uid = 0;
2412 SYSCTL_INT(_security, OID_AUTO, hardlink_check_uid, CTLFLAG_RW,
2413     &hardlink_check_uid, 0,
2414     "Unprivileged processes cannot create hard links to files owned by other "
2415     "users");
2416 static int hardlink_check_gid = 0;
2417 SYSCTL_INT(_security, OID_AUTO, hardlink_check_gid, CTLFLAG_RW,
2418     &hardlink_check_gid, 0,
2419     "Unprivileged processes cannot create hard links to files owned by other "
2420     "groups");
2421 
2422 static int
2423 can_hardlink(struct vnode *vp, struct thread *td, struct ucred *cred)
2424 {
2425 	struct vattr va;
2426 	int error;
2427 
2428 	/*
2429 	 * Shortcut if disabled
2430 	 */
2431 	if (hardlink_check_uid == 0 && hardlink_check_gid == 0)
2432 		return (0);
2433 
2434 	/*
2435 	 * Privileged user can always hardlink
2436 	 */
2437 	if (priv_check_cred(cred, PRIV_VFS_LINK, 0) == 0)
2438 		return (0);
2439 
2440 	/*
2441 	 * Otherwise only if the originating file is owned by the
2442 	 * same user or group.  Note that any group is allowed if
2443 	 * the file is owned by the caller.
2444 	 */
2445 	error = VOP_GETATTR(vp, &va);
2446 	if (error != 0)
2447 		return (error);
2448 
2449 	if (hardlink_check_uid) {
2450 		if (cred->cr_uid != va.va_uid)
2451 			return (EPERM);
2452 	}
2453 
2454 	if (hardlink_check_gid) {
2455 		if (cred->cr_uid != va.va_uid && !groupmember(va.va_gid, cred))
2456 			return (EPERM);
2457 	}
2458 
2459 	return (0);
2460 }
2461 
2462 int
2463 kern_link(struct nlookupdata *nd, struct nlookupdata *linknd)
2464 {
2465 	struct thread *td = curthread;
2466 	struct vnode *vp;
2467 	int error;
2468 
2469 	/*
2470 	 * Lookup the source and obtained a locked vnode.
2471 	 *
2472 	 * You may only hardlink a file which you have write permission
2473 	 * on or which you own.
2474 	 *
2475 	 * XXX relookup on vget failure / race ?
2476 	 */
2477 	bwillinode(1);
2478 	nd->nl_flags |= NLC_WRITE | NLC_OWN | NLC_HLINK;
2479 	if ((error = nlookup(nd)) != 0)
2480 		return (error);
2481 	vp = nd->nl_nch.ncp->nc_vp;
2482 	KKASSERT(vp != NULL);
2483 	if (vp->v_type == VDIR)
2484 		return (EPERM);		/* POSIX */
2485 	if ((error = ncp_writechk(&nd->nl_nch)) != 0)
2486 		return (error);
2487 	if ((error = vget(vp, LK_EXCLUSIVE)) != 0)
2488 		return (error);
2489 
2490 	/*
2491 	 * Unlock the source so we can lookup the target without deadlocking
2492 	 * (XXX vp is locked already, possible other deadlock?).  The target
2493 	 * must not exist.
2494 	 */
2495 	KKASSERT(nd->nl_flags & NLC_NCPISLOCKED);
2496 	nd->nl_flags &= ~NLC_NCPISLOCKED;
2497 	cache_unlock(&nd->nl_nch);
2498 	vn_unlock(vp);
2499 
2500 	linknd->nl_flags |= NLC_CREATE | NLC_REFDVP;
2501 	if ((error = nlookup(linknd)) != 0) {
2502 		vrele(vp);
2503 		return (error);
2504 	}
2505 	if (linknd->nl_nch.ncp->nc_vp) {
2506 		vrele(vp);
2507 		return (EEXIST);
2508 	}
2509 	if (linknd->nl_dvp == NULL) {
2510 		vrele(vp);
2511 		return (EINVAL);
2512 	}
2513 	VFS_MODIFYING(vp->v_mount);
2514 	error = vn_lock(vp, LK_EXCLUSIVE | LK_RETRY | LK_FAILRECLAIM);
2515 	if (error) {
2516 		vrele(vp);
2517 		return (error);
2518 	}
2519 
2520 	/*
2521 	 * Finally run the new API VOP.
2522 	 */
2523 	error = can_hardlink(vp, td, td->td_ucred);
2524 	if (error == 0) {
2525 		error = VOP_NLINK(&linknd->nl_nch, linknd->nl_dvp,
2526 				  vp, linknd->nl_cred);
2527 	}
2528 	vput(vp);
2529 	return (error);
2530 }
2531 
2532 /*
2533  * link_args(char *path, char *link)
2534  *
2535  * Make a hard file link.
2536  */
2537 int
2538 sys_link(struct sysmsg *sysmsg, const struct link_args *uap)
2539 {
2540 	struct nlookupdata nd, linknd;
2541 	int error;
2542 
2543 	error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
2544 	if (error == 0) {
2545 		error = nlookup_init(&linknd, uap->link, UIO_USERSPACE, 0);
2546 		if (error == 0)
2547 			error = kern_link(&nd, &linknd);
2548 		nlookup_done(&linknd);
2549 	}
2550 	nlookup_done(&nd);
2551 	return (error);
2552 }
2553 
2554 /*
2555  * linkat_args(int fd1, char *path1, int fd2, char *path2, int flags)
2556  *
2557  * Make a hard file link. The path1 argument is relative to the directory
2558  * associated with fd1, and similarly the path2 argument is relative to
2559  * the directory associated with fd2.
2560  */
2561 int
2562 sys_linkat(struct sysmsg *sysmsg, const struct linkat_args *uap)
2563 {
2564 	struct nlookupdata nd, linknd;
2565 	struct file *fp1, *fp2;
2566 	int error;
2567 
2568 	error = nlookup_init_at(&nd, &fp1, uap->fd1, uap->path1, UIO_USERSPACE,
2569 	    (uap->flags & AT_SYMLINK_FOLLOW) ? NLC_FOLLOW : 0);
2570 	if (error == 0) {
2571 		error = nlookup_init_at(&linknd, &fp2, uap->fd2,
2572 		    uap->path2, UIO_USERSPACE, 0);
2573 		if (error == 0)
2574 			error = kern_link(&nd, &linknd);
2575 		nlookup_done_at(&linknd, fp2);
2576 	}
2577 	nlookup_done_at(&nd, fp1);
2578 	return (error);
2579 }
2580 
2581 int
2582 kern_symlink(struct nlookupdata *nd, char *path, int mode)
2583 {
2584 	struct vattr vattr;
2585 	struct vnode *vp;
2586 	struct vnode *dvp;
2587 	int error;
2588 
2589 	bwillinode(1);
2590 	nd->nl_flags |= NLC_CREATE | NLC_REFDVP;
2591 	if ((error = nlookup(nd)) != 0)
2592 		return (error);
2593 	if (nd->nl_nch.ncp->nc_vp)
2594 		return (EEXIST);
2595 	if (nd->nl_dvp == NULL)
2596 		return (EINVAL);
2597 	if ((error = ncp_writechk(&nd->nl_nch)) != 0)
2598 		return (error);
2599 	dvp = nd->nl_dvp;
2600 	VATTR_NULL(&vattr);
2601 	vattr.va_mode = mode;
2602 	error = VOP_NSYMLINK(&nd->nl_nch, dvp, &vp, nd->nl_cred, &vattr, path);
2603 	if (error == 0)
2604 		vput(vp);
2605 	return (error);
2606 }
2607 
2608 /*
2609  * symlink(char *path, char *link)
2610  *
2611  * Make a symbolic link.
2612  */
2613 int
2614 sys_symlink(struct sysmsg *sysmsg, const struct symlink_args *uap)
2615 {
2616 	struct thread *td = curthread;
2617 	struct nlookupdata nd;
2618 	char *path;
2619 	int error;
2620 	int mode;
2621 
2622 	path = objcache_get(namei_oc, M_WAITOK);
2623 	error = copyinstr(uap->path, path, MAXPATHLEN, NULL);
2624 	if (error == 0) {
2625 		error = nlookup_init(&nd, uap->link, UIO_USERSPACE, 0);
2626 		if (error == 0) {
2627 			mode = ACCESSPERMS & ~td->td_proc->p_fd->fd_cmask;
2628 			error = kern_symlink(&nd, path, mode);
2629 		}
2630 		nlookup_done(&nd);
2631 	}
2632 	objcache_put(namei_oc, path);
2633 	return (error);
2634 }
2635 
2636 /*
2637  * symlinkat_args(char *path1, int fd, char *path2)
2638  *
2639  * Make a symbolic link.  The path2 argument is relative to the directory
2640  * associated with fd.
2641  */
2642 int
2643 sys_symlinkat(struct sysmsg *sysmsg, const struct symlinkat_args *uap)
2644 {
2645 	struct thread *td = curthread;
2646 	struct nlookupdata nd;
2647 	struct file *fp;
2648 	char *path1;
2649 	int error;
2650 	int mode;
2651 
2652 	path1 = objcache_get(namei_oc, M_WAITOK);
2653 	error = copyinstr(uap->path1, path1, MAXPATHLEN, NULL);
2654 	if (error == 0) {
2655 		error = nlookup_init_at(&nd, &fp, uap->fd, uap->path2,
2656 		    UIO_USERSPACE, 0);
2657 		if (error == 0) {
2658 			mode = ACCESSPERMS & ~td->td_proc->p_fd->fd_cmask;
2659 			error = kern_symlink(&nd, path1, mode);
2660 		}
2661 		nlookup_done_at(&nd, fp);
2662 	}
2663 	objcache_put(namei_oc, path1);
2664 	return (error);
2665 }
2666 
2667 /*
2668  * undelete_args(char *path)
2669  *
2670  * Delete a whiteout from the filesystem.
2671  */
2672 int
2673 sys_undelete(struct sysmsg *sysmsg, const struct undelete_args *uap)
2674 {
2675 	struct nlookupdata nd;
2676 	int error;
2677 
2678 	error = nlookup_init(&nd, uap->path, UIO_USERSPACE, 0);
2679 	bwillinode(1);
2680 	nd.nl_flags |= NLC_DELETE | NLC_REFDVP;
2681 	if (error == 0)
2682 		error = nlookup(&nd);
2683 	if (error == 0 && nd.nl_dvp == NULL)
2684 		error = EINVAL;
2685 	if (error == 0)
2686 		error = ncp_writechk(&nd.nl_nch);
2687 	if (error == 0) {
2688 		error = VOP_NWHITEOUT(&nd.nl_nch, nd.nl_dvp, nd.nl_cred,
2689 				      NAMEI_DELETE);
2690 	}
2691 	nlookup_done(&nd);
2692 	return (error);
2693 }
2694 
2695 int
2696 kern_unlink(struct nlookupdata *nd)
2697 {
2698 	int error;
2699 
2700 	bwillinode(1);
2701 	nd->nl_flags |= NLC_DELETE | NLC_REFDVP;
2702 	if ((error = nlookup(nd)) != 0)
2703 		return (error);
2704 	if (nd->nl_dvp == NULL)
2705 		return EINVAL;
2706 	if ((error = ncp_writechk(&nd->nl_nch)) != 0)
2707 		return (error);
2708 	error = VOP_NREMOVE(&nd->nl_nch, nd->nl_dvp, nd->nl_cred);
2709 	return (error);
2710 }
2711 
2712 /*
2713  * unlink_args(char *path)
2714  *
2715  * Delete a name from the filesystem.
2716  */
2717 int
2718 sys_unlink(struct sysmsg *sysmsg, const struct unlink_args *uap)
2719 {
2720 	struct nlookupdata nd;
2721 	int error;
2722 
2723 	error = nlookup_init(&nd, uap->path, UIO_USERSPACE, 0);
2724 	if (error == 0)
2725 		error = kern_unlink(&nd);
2726 	nlookup_done(&nd);
2727 	return (error);
2728 }
2729 
2730 
2731 /*
2732  * unlinkat_args(int fd, char *path, int flags)
2733  *
2734  * Delete the file or directory entry pointed to by fd/path.
2735  */
2736 int
2737 sys_unlinkat(struct sysmsg *sysmsg, const struct unlinkat_args *uap)
2738 {
2739 	struct nlookupdata nd;
2740 	struct file *fp;
2741 	int error;
2742 
2743 	if (uap->flags & ~AT_REMOVEDIR)
2744 		return (EINVAL);
2745 
2746 	error = nlookup_init_at(&nd, &fp, uap->fd, uap->path, UIO_USERSPACE, 0);
2747 	if (error == 0) {
2748 		if (uap->flags & AT_REMOVEDIR)
2749 			error = kern_rmdir(&nd);
2750 		else
2751 			error = kern_unlink(&nd);
2752 	}
2753 	nlookup_done_at(&nd, fp);
2754 	return (error);
2755 }
2756 
2757 int
2758 kern_lseek(int fd, off_t offset, int whence, off_t *res)
2759 {
2760 	struct thread *td = curthread;
2761 	struct file *fp;
2762 	struct vnode *vp;
2763 	struct vattr_lite lva;
2764 	off_t new_offset;
2765 	int error;
2766 
2767 	fp = holdfp(td, fd, -1);
2768 	if (fp == NULL)
2769 		return (EBADF);
2770 	if (fp->f_type != DTYPE_VNODE) {
2771 		error = ESPIPE;
2772 		goto done;
2773 	}
2774 	vp = (struct vnode *)fp->f_data;
2775 
2776 	switch (whence) {
2777 	case L_INCR:
2778 		spin_lock(&fp->f_spin);
2779 		new_offset = fp->f_offset + offset;
2780 		error = 0;
2781 		break;
2782 	case L_XTND:
2783 		error = VOP_GETATTR_LITE(vp, &lva);
2784 		spin_lock(&fp->f_spin);
2785 		new_offset = offset + lva.va_size;
2786 		break;
2787 	case L_SET:
2788 		new_offset = offset;
2789 		error = 0;
2790 		spin_lock(&fp->f_spin);
2791 		break;
2792 	default:
2793 		new_offset = 0;
2794 		error = EINVAL;
2795 		spin_lock(&fp->f_spin);
2796 		break;
2797 	}
2798 
2799 	/*
2800 	 * Validate the seek position.  Negative offsets are not allowed
2801 	 * for regular files or directories.
2802 	 *
2803 	 * Normally we would also not want to allow negative offsets for
2804 	 * character and block-special devices.  However kvm addresses
2805 	 * on 64 bit architectures might appear to be negative and must
2806 	 * be allowed.
2807 	 */
2808 	if (error == 0) {
2809 		if (new_offset < 0 &&
2810 		    (vp->v_type == VREG || vp->v_type == VDIR)) {
2811 			error = EINVAL;
2812 		} else {
2813 			fp->f_offset = new_offset;
2814 		}
2815 	}
2816 	*res = fp->f_offset;
2817 	spin_unlock(&fp->f_spin);
2818 done:
2819 	dropfp(td, fd, fp);
2820 
2821 	return (error);
2822 }
2823 
2824 /*
2825  * lseek_args(int fd, int pad, off_t offset, int whence)
2826  *
2827  * Reposition read/write file offset.
2828  */
2829 int
2830 sys_lseek(struct sysmsg *sysmsg, const struct lseek_args *uap)
2831 {
2832 	int error;
2833 
2834 	error = kern_lseek(uap->fd, uap->offset, uap->whence,
2835 			   &sysmsg->sysmsg_offset);
2836 
2837 	return (error);
2838 }
2839 
2840 /*
2841  * Check if current process can access given file.  amode is a bitmask of *_OK
2842  * access bits.  flags is a bitmask of AT_* flags.
2843  */
2844 int
2845 kern_access(struct nlookupdata *nd, int amode, int flags)
2846 {
2847 	struct vnode *vp;
2848 	int error, mode;
2849 
2850 	if (flags & ~AT_EACCESS)
2851 		return (EINVAL);
2852 	nd->nl_flags |= NLC_SHAREDLOCK;
2853 	if ((error = nlookup(nd)) != 0)
2854 		return (error);
2855 	if ((amode & W_OK) && (error = ncp_writechk(&nd->nl_nch)) != 0)
2856 		return (error);
2857 retry:
2858 	error = cache_vget(&nd->nl_nch, nd->nl_cred, LK_SHARED, &vp);
2859 	if (error)
2860 		return (error);
2861 
2862 	/* Flags == 0 means only check for existence. */
2863 	if (amode) {
2864 		mode = 0;
2865 		if (amode & R_OK)
2866 			mode |= VREAD;
2867 		if (amode & W_OK)
2868 			mode |= VWRITE;
2869 		if (amode & X_OK)
2870 			mode |= VEXEC;
2871 		if ((mode & VWRITE) == 0 ||
2872 		    (error = vn_writechk(vp)) == 0) {
2873 			error = VOP_ACCESS_FLAGS(vp, mode, flags, nd->nl_cred);
2874 		}
2875 
2876 		/*
2877 		 * If the file handle is stale we have to re-resolve the
2878 		 * entry with the ncp held exclusively.  This is a hack
2879 		 * at the moment.
2880 		 */
2881 		if (error == ESTALE) {
2882 			vput(vp);
2883 			cache_unlock(&nd->nl_nch);
2884 			cache_lock(&nd->nl_nch);
2885 			cache_setunresolved(&nd->nl_nch);
2886 			error = cache_resolve(&nd->nl_nch, nd->nl_cred);
2887 			if (error == 0) {
2888 				vp = NULL;
2889 				goto retry;
2890 			}
2891 			return(error);
2892 		}
2893 	}
2894 	vput(vp);
2895 	return (error);
2896 }
2897 
2898 /*
2899  * access_args(char *path, int flags)
2900  *
2901  * Check access permissions.
2902  */
2903 int
2904 sys_access(struct sysmsg *sysmsg, const struct access_args *uap)
2905 {
2906 	struct nlookupdata nd;
2907 	int error;
2908 
2909 	error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
2910 	if (error == 0)
2911 		error = kern_access(&nd, uap->flags, 0);
2912 	nlookup_done(&nd);
2913 	return (error);
2914 }
2915 
2916 
2917 /*
2918  * eaccess_args(char *path, int flags)
2919  *
2920  * Check access permissions.
2921  */
2922 int
2923 sys_eaccess(struct sysmsg *sysmsg, const struct eaccess_args *uap)
2924 {
2925 	struct nlookupdata nd;
2926 	int error;
2927 
2928 	error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
2929 	if (error == 0)
2930 		error = kern_access(&nd, uap->flags, AT_EACCESS);
2931 	nlookup_done(&nd);
2932 	return (error);
2933 }
2934 
2935 
2936 /*
2937  * faccessat_args(int fd, char *path, int amode, int flags)
2938  *
2939  * Check access permissions.
2940  */
2941 int
2942 sys_faccessat(struct sysmsg *sysmsg, const struct faccessat_args *uap)
2943 {
2944 	struct nlookupdata nd;
2945 	struct file *fp;
2946 	int error;
2947 
2948 	error = nlookup_init_at(&nd, &fp, uap->fd, uap->path, UIO_USERSPACE,
2949 				NLC_FOLLOW);
2950 	if (error == 0)
2951 		error = kern_access(&nd, uap->amode, uap->flags);
2952 	nlookup_done_at(&nd, fp);
2953 	return (error);
2954 }
2955 
2956 int
2957 kern_stat(struct nlookupdata *nd, struct stat *st)
2958 {
2959 	int error;
2960 	struct vnode *vp;
2961 
2962 	nd->nl_flags |= NLC_SHAREDLOCK;
2963 	if ((error = nlookup(nd)) != 0)
2964 		return (error);
2965 again:
2966 	if ((vp = nd->nl_nch.ncp->nc_vp) == NULL)
2967 		return (ENOENT);
2968 
2969 #if 1
2970 	error = cache_vref(&nd->nl_nch, NULL, &vp);
2971 #else
2972 	error = vget(vp, LK_SHARED);
2973 #endif
2974 	if (error)
2975 		return (error);
2976 	error = vn_stat(vp, st, nd->nl_cred);
2977 
2978 	/*
2979 	 * If the file handle is stale we have to re-resolve the
2980 	 * entry with the ncp held exclusively.  This is a hack
2981 	 * at the moment.
2982 	 */
2983 	if (error == ESTALE) {
2984 #if 1
2985 		vrele(vp);
2986 #else
2987 		vput(vp);
2988 #endif
2989 		cache_unlock(&nd->nl_nch);
2990 		cache_lock(&nd->nl_nch);
2991 		cache_setunresolved(&nd->nl_nch);
2992 		error = cache_resolve(&nd->nl_nch, nd->nl_cred);
2993 		if (error == 0)
2994 			goto again;
2995 	} else {
2996 #if 1
2997 		vrele(vp);
2998 #else
2999 		vput(vp);
3000 #endif
3001 	}
3002 	return (error);
3003 }
3004 
3005 /*
3006  * stat_args(char *path, struct stat *ub)
3007  *
3008  * Get file status; this version follows links.
3009  */
3010 int
3011 sys_stat(struct sysmsg *sysmsg, const struct stat_args *uap)
3012 {
3013 	struct nlookupdata nd;
3014 	struct stat st;
3015 	int error;
3016 
3017 	error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
3018 	if (error == 0) {
3019 		error = kern_stat(&nd, &st);
3020 		if (error == 0)
3021 			error = copyout(&st, uap->ub, sizeof(*uap->ub));
3022 	}
3023 	nlookup_done(&nd);
3024 	return (error);
3025 }
3026 
3027 /*
3028  * lstat_args(char *path, struct stat *ub)
3029  *
3030  * Get file status; this version does not follow links.
3031  */
3032 int
3033 sys_lstat(struct sysmsg *sysmsg, const struct lstat_args *uap)
3034 {
3035 	struct nlookupdata nd;
3036 	struct stat st;
3037 	int error;
3038 
3039 	error = nlookup_init(&nd, uap->path, UIO_USERSPACE, 0);
3040 	if (error == 0) {
3041 		error = kern_stat(&nd, &st);
3042 		if (error == 0)
3043 			error = copyout(&st, uap->ub, sizeof(*uap->ub));
3044 	}
3045 	nlookup_done(&nd);
3046 	return (error);
3047 }
3048 
3049 /*
3050  * fstatat_args(int fd, char *path, struct stat *sb, int flags)
3051  *
3052  * Get status of file pointed to by fd/path.
3053  */
3054 int
3055 sys_fstatat(struct sysmsg *sysmsg, const struct fstatat_args *uap)
3056 {
3057 	struct nlookupdata nd;
3058 	struct stat st;
3059 	int error;
3060 	int flags;
3061 	struct file *fp;
3062 
3063 	if (uap->flags & ~AT_SYMLINK_NOFOLLOW)
3064 		return (EINVAL);
3065 
3066 	flags = (uap->flags & AT_SYMLINK_NOFOLLOW) ? 0 : NLC_FOLLOW;
3067 
3068 	error = nlookup_init_at(&nd, &fp, uap->fd, uap->path,
3069 				UIO_USERSPACE, flags);
3070 	if (error == 0) {
3071 		error = kern_stat(&nd, &st);
3072 		if (error == 0)
3073 			error = copyout(&st, uap->sb, sizeof(*uap->sb));
3074 	}
3075 	nlookup_done_at(&nd, fp);
3076 	return (error);
3077 }
3078 
3079 static int
3080 kern_pathconf(char *path, int name, int flags, register_t *sysmsg_regp)
3081 {
3082 	struct nlookupdata nd;
3083 	struct vnode *vp;
3084 	int error;
3085 
3086 	vp = NULL;
3087 	error = nlookup_init(&nd, path, UIO_USERSPACE, flags);
3088 	if (error == 0)
3089 		error = nlookup(&nd);
3090 	if (error == 0)
3091 		error = cache_vget(&nd.nl_nch, nd.nl_cred, LK_EXCLUSIVE, &vp);
3092 	nlookup_done(&nd);
3093 	if (error == 0) {
3094 		error = VOP_PATHCONF(vp, name, sysmsg_regp);
3095 		vput(vp);
3096 	}
3097 	return (error);
3098 }
3099 
3100 /*
3101  * pathconf_Args(char *path, int name)
3102  *
3103  * Get configurable pathname variables.
3104  */
3105 int
3106 sys_pathconf(struct sysmsg *sysmsg, const struct pathconf_args *uap)
3107 {
3108 	return (kern_pathconf(uap->path, uap->name, NLC_FOLLOW,
3109 		&sysmsg->sysmsg_reg));
3110 }
3111 
3112 /*
3113  * lpathconf_Args(char *path, int name)
3114  *
3115  * Get configurable pathname variables, but don't follow symlinks.
3116  */
3117 int
3118 sys_lpathconf(struct sysmsg *sysmsg, const struct lpathconf_args *uap)
3119 {
3120 	return (kern_pathconf(uap->path, uap->name, 0, &sysmsg->sysmsg_reg));
3121 }
3122 
3123 /*
3124  * XXX: daver
3125  * kern_readlink isn't properly split yet.  There is a copyin burried
3126  * in VOP_READLINK().
3127  */
3128 int
3129 kern_readlink(struct nlookupdata *nd, char *buf, int count, int *res)
3130 {
3131 	struct thread *td = curthread;
3132 	struct vnode *vp;
3133 	struct iovec aiov;
3134 	struct uio auio;
3135 	int error;
3136 
3137 	nd->nl_flags |= NLC_SHAREDLOCK;
3138 	if ((error = nlookup(nd)) != 0)
3139 		return (error);
3140 	error = cache_vget(&nd->nl_nch, nd->nl_cred, LK_SHARED, &vp);
3141 	if (error)
3142 		return (error);
3143 	if (vp->v_type != VLNK) {
3144 		error = EINVAL;
3145 	} else {
3146 		aiov.iov_base = buf;
3147 		aiov.iov_len = count;
3148 		auio.uio_iov = &aiov;
3149 		auio.uio_iovcnt = 1;
3150 		auio.uio_offset = 0;
3151 		auio.uio_rw = UIO_READ;
3152 		auio.uio_segflg = UIO_USERSPACE;
3153 		auio.uio_td = td;
3154 		auio.uio_resid = count;
3155 		error = VOP_READLINK(vp, &auio, td->td_ucred);
3156 	}
3157 	vput(vp);
3158 	*res = count - auio.uio_resid;
3159 	return (error);
3160 }
3161 
3162 /*
3163  * readlink_args(char *path, char *buf, int count)
3164  *
3165  * Return target name of a symbolic link.
3166  */
3167 int
3168 sys_readlink(struct sysmsg *sysmsg, const struct readlink_args *uap)
3169 {
3170 	struct nlookupdata nd;
3171 	int error;
3172 
3173 	error = nlookup_init(&nd, uap->path, UIO_USERSPACE, 0);
3174 	if (error == 0) {
3175 		error = kern_readlink(&nd, uap->buf, uap->count,
3176 					&sysmsg->sysmsg_result);
3177 	}
3178 	nlookup_done(&nd);
3179 	return (error);
3180 }
3181 
3182 /*
3183  * readlinkat_args(int fd, char *path, char *buf, size_t bufsize)
3184  *
3185  * Return target name of a symbolic link.  The path is relative to the
3186  * directory associated with fd.
3187  */
3188 int
3189 sys_readlinkat(struct sysmsg *sysmsg, const struct readlinkat_args *uap)
3190 {
3191 	struct nlookupdata nd;
3192 	struct file *fp;
3193 	int error;
3194 
3195 	error = nlookup_init_at(&nd, &fp, uap->fd, uap->path, UIO_USERSPACE, 0);
3196 	if (error == 0) {
3197 		error = kern_readlink(&nd, uap->buf, uap->bufsize,
3198 					&sysmsg->sysmsg_result);
3199 	}
3200 	nlookup_done_at(&nd, fp);
3201 	return (error);
3202 }
3203 
3204 static int
3205 setfflags(struct vnode *vp, u_long flags)
3206 {
3207 	struct thread *td = curthread;
3208 	int error;
3209 	struct vattr vattr;
3210 
3211 	/*
3212 	 * Prevent non-root users from setting flags on devices.  When
3213 	 * a device is reused, users can retain ownership of the device
3214 	 * if they are allowed to set flags and programs assume that
3215 	 * chown can't fail when done as root.
3216 	 */
3217 	if ((vp->v_type == VCHR || vp->v_type == VBLK) &&
3218 	    ((error = priv_check_cred(td->td_ucred, PRIV_VFS_CHFLAGS_DEV, 0)) != 0))
3219 		return (error);
3220 
3221 	/*
3222 	 * note: vget is required for any operation that might mod the vnode
3223 	 * so VINACTIVE is properly cleared.
3224 	 */
3225 	if ((error = vget(vp, LK_EXCLUSIVE)) == 0) {
3226 		VATTR_NULL(&vattr);
3227 		vattr.va_flags = flags;
3228 		error = VOP_SETATTR(vp, &vattr, td->td_ucred);
3229 		vput(vp);
3230 	}
3231 	return (error);
3232 }
3233 
3234 /*
3235  * chflags(const char *path, u_long flags)
3236  *
3237  * Change flags of a file given a path name.
3238  */
3239 int
3240 sys_chflags(struct sysmsg *sysmsg, const struct chflags_args *uap)
3241 {
3242 	struct nlookupdata nd;
3243 	struct vnode *vp;
3244 	int error;
3245 
3246 	vp = NULL;
3247 	error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
3248 	if (error == 0)
3249 		error = nlookup(&nd);
3250 	if (error == 0)
3251 		error = ncp_writechk(&nd.nl_nch);
3252 	if (error == 0)
3253 		error = cache_vref(&nd.nl_nch, nd.nl_cred, &vp);
3254 	nlookup_done(&nd);
3255 	if (error == 0) {
3256 		error = setfflags(vp, uap->flags);
3257 		vrele(vp);
3258 	}
3259 	return (error);
3260 }
3261 
3262 /*
3263  * lchflags(const char *path, u_long flags)
3264  *
3265  * Change flags of a file given a path name, but don't follow symlinks.
3266  */
3267 int
3268 sys_lchflags(struct sysmsg *sysmsg, const struct lchflags_args *uap)
3269 {
3270 	struct nlookupdata nd;
3271 	struct vnode *vp;
3272 	int error;
3273 
3274 	vp = NULL;
3275 	error = nlookup_init(&nd, uap->path, UIO_USERSPACE, 0);
3276 	if (error == 0)
3277 		error = nlookup(&nd);
3278 	if (error == 0)
3279 		error = ncp_writechk(&nd.nl_nch);
3280 	if (error == 0)
3281 		error = cache_vref(&nd.nl_nch, nd.nl_cred, &vp);
3282 	nlookup_done(&nd);
3283 	if (error == 0) {
3284 		error = setfflags(vp, uap->flags);
3285 		vrele(vp);
3286 	}
3287 	return (error);
3288 }
3289 
3290 /*
3291  * fchflags_args(int fd, u_flags flags)
3292  *
3293  * Change flags of a file given a file descriptor.
3294  */
3295 int
3296 sys_fchflags(struct sysmsg *sysmsg, const struct fchflags_args *uap)
3297 {
3298 	struct thread *td = curthread;
3299 	struct file *fp;
3300 	int error;
3301 
3302 	if ((error = holdvnode(td, uap->fd, &fp)) != 0)
3303 		return (error);
3304 	if (fp->f_nchandle.ncp)
3305 		error = ncp_writechk(&fp->f_nchandle);
3306 	if (error == 0)
3307 		error = setfflags((struct vnode *) fp->f_data, uap->flags);
3308 	fdrop(fp);
3309 	return (error);
3310 }
3311 
3312 /*
3313  * chflagsat_args(int fd, const char *path, u_long flags, int atflags)
3314  * change flags given a pathname relative to a filedescriptor
3315  */
3316 int
3317 sys_chflagsat(struct sysmsg *sysmsg, const struct chflagsat_args *uap)
3318 {
3319 	struct nlookupdata nd;
3320 	struct vnode *vp;
3321 	struct file *fp;
3322 	int error;
3323 	int lookupflags;
3324 
3325 	if (uap->atflags & ~AT_SYMLINK_NOFOLLOW)
3326 		return (EINVAL);
3327 
3328 	lookupflags = (uap->atflags & AT_SYMLINK_NOFOLLOW) ? 0 : NLC_FOLLOW;
3329 
3330 	vp = NULL;
3331 	error = nlookup_init_at(&nd, &fp, uap->fd,  uap->path, UIO_USERSPACE, lookupflags);
3332 	if (error == 0)
3333 		error = nlookup(&nd);
3334 	if (error == 0)
3335 		error = ncp_writechk(&nd.nl_nch);
3336 	if (error == 0)
3337 		error = cache_vref(&nd.nl_nch, nd.nl_cred, &vp);
3338 	nlookup_done_at(&nd, fp);
3339 	if (error == 0) {
3340 		error = setfflags(vp, uap->flags);
3341 		vrele(vp);
3342 	}
3343 	return (error);
3344 }
3345 
3346 
3347 static int
3348 setfmode(struct vnode *vp, int mode)
3349 {
3350 	struct thread *td = curthread;
3351 	int error;
3352 	struct vattr vattr;
3353 
3354 	/*
3355 	 * note: vget is required for any operation that might mod the vnode
3356 	 * so VINACTIVE is properly cleared.
3357 	 */
3358 	if ((error = vget(vp, LK_EXCLUSIVE)) == 0) {
3359 		VATTR_NULL(&vattr);
3360 		vattr.va_mode = mode & ALLPERMS;
3361 		error = VOP_SETATTR(vp, &vattr, td->td_ucred);
3362 		cache_inval_wxok(vp);
3363 		vput(vp);
3364 	}
3365 	return error;
3366 }
3367 
3368 int
3369 kern_chmod(struct nlookupdata *nd, int mode)
3370 {
3371 	struct vnode *vp;
3372 	int error;
3373 
3374 	if ((error = nlookup(nd)) != 0)
3375 		return (error);
3376 	if ((error = cache_vref(&nd->nl_nch, nd->nl_cred, &vp)) != 0)
3377 		return (error);
3378 	if ((error = ncp_writechk(&nd->nl_nch)) == 0)
3379 		error = setfmode(vp, mode);
3380 	vrele(vp);
3381 	return (error);
3382 }
3383 
3384 /*
3385  * chmod_args(char *path, int mode)
3386  *
3387  * Change mode of a file given path name.
3388  */
3389 int
3390 sys_chmod(struct sysmsg *sysmsg, const struct chmod_args *uap)
3391 {
3392 	struct nlookupdata nd;
3393 	int error;
3394 
3395 	error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
3396 	if (error == 0)
3397 		error = kern_chmod(&nd, uap->mode);
3398 	nlookup_done(&nd);
3399 	return (error);
3400 }
3401 
3402 /*
3403  * lchmod_args(char *path, int mode)
3404  *
3405  * Change mode of a file given path name (don't follow links.)
3406  */
3407 int
3408 sys_lchmod(struct sysmsg *sysmsg, const struct lchmod_args *uap)
3409 {
3410 	struct nlookupdata nd;
3411 	int error;
3412 
3413 	error = nlookup_init(&nd, uap->path, UIO_USERSPACE, 0);
3414 	if (error == 0)
3415 		error = kern_chmod(&nd, uap->mode);
3416 	nlookup_done(&nd);
3417 	return (error);
3418 }
3419 
3420 /*
3421  * fchmod_args(int fd, int mode)
3422  *
3423  * Change mode of a file given a file descriptor.
3424  */
3425 int
3426 sys_fchmod(struct sysmsg *sysmsg, const struct fchmod_args *uap)
3427 {
3428 	struct thread *td = curthread;
3429 	struct file *fp;
3430 	int error;
3431 
3432 	if ((error = holdvnode(td, uap->fd, &fp)) != 0)
3433 		return (error);
3434 	if (fp->f_nchandle.ncp)
3435 		error = ncp_writechk(&fp->f_nchandle);
3436 	if (error == 0)
3437 		error = setfmode((struct vnode *)fp->f_data, uap->mode);
3438 	fdrop(fp);
3439 	return (error);
3440 }
3441 
3442 /*
3443  * fchmodat_args(char *path, int mode)
3444  *
3445  * Change mode of a file pointed to by fd/path.
3446  */
3447 int
3448 sys_fchmodat(struct sysmsg *sysmsg, const struct fchmodat_args *uap)
3449 {
3450 	struct nlookupdata nd;
3451 	struct file *fp;
3452 	int error;
3453 	int flags;
3454 
3455 	if (uap->flags & ~AT_SYMLINK_NOFOLLOW)
3456 		return (EINVAL);
3457 	flags = (uap->flags & AT_SYMLINK_NOFOLLOW) ? 0 : NLC_FOLLOW;
3458 
3459 	error = nlookup_init_at(&nd, &fp, uap->fd, uap->path,
3460 				UIO_USERSPACE, flags);
3461 	if (error == 0)
3462 		error = kern_chmod(&nd, uap->mode);
3463 	nlookup_done_at(&nd, fp);
3464 	return (error);
3465 }
3466 
3467 static int
3468 setfown(struct mount *mp, struct vnode *vp, uid_t uid, gid_t gid)
3469 {
3470 	struct thread *td = curthread;
3471 	int error;
3472 	struct vattr vattr;
3473 	uid_t o_uid;
3474 	gid_t o_gid;
3475 	uint64_t size;
3476 
3477 	/*
3478 	 * note: vget is required for any operation that might mod the vnode
3479 	 * so VINACTIVE is properly cleared.
3480 	 */
3481 	if ((error = vget(vp, LK_EXCLUSIVE)) == 0) {
3482 		if ((error = VOP_GETATTR(vp, &vattr)) != 0)
3483 			return error;
3484 		o_uid = vattr.va_uid;
3485 		o_gid = vattr.va_gid;
3486 		size = vattr.va_size;
3487 
3488 		VATTR_NULL(&vattr);
3489 		vattr.va_uid = uid;
3490 		vattr.va_gid = gid;
3491 		error = VOP_SETATTR(vp, &vattr, td->td_ucred);
3492 		vput(vp);
3493 	}
3494 
3495 	if (error == 0) {
3496 		if (uid == -1)
3497 			uid = o_uid;
3498 		if (gid == -1)
3499 			gid = o_gid;
3500 		VFS_ACCOUNT(mp, o_uid, o_gid, -size);
3501 		VFS_ACCOUNT(mp,   uid,   gid,  size);
3502 	}
3503 
3504 	return error;
3505 }
3506 
3507 int
3508 kern_chown(struct nlookupdata *nd, int uid, int gid)
3509 {
3510 	struct vnode *vp;
3511 	int error;
3512 
3513 	if ((error = nlookup(nd)) != 0)
3514 		return (error);
3515 	if ((error = cache_vref(&nd->nl_nch, nd->nl_cred, &vp)) != 0)
3516 		return (error);
3517 	if ((error = ncp_writechk(&nd->nl_nch)) == 0)
3518 		error = setfown(nd->nl_nch.mount, vp, uid, gid);
3519 	vrele(vp);
3520 	return (error);
3521 }
3522 
3523 /*
3524  * chown(char *path, int uid, int gid)
3525  *
3526  * Set ownership given a path name.
3527  */
3528 int
3529 sys_chown(struct sysmsg *sysmsg, const struct chown_args *uap)
3530 {
3531 	struct nlookupdata nd;
3532 	int error;
3533 
3534 	error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
3535 	if (error == 0)
3536 		error = kern_chown(&nd, uap->uid, uap->gid);
3537 	nlookup_done(&nd);
3538 	return (error);
3539 }
3540 
3541 /*
3542  * lchown_args(char *path, int uid, int gid)
3543  *
3544  * Set ownership given a path name, do not cross symlinks.
3545  */
3546 int
3547 sys_lchown(struct sysmsg *sysmsg, const struct lchown_args *uap)
3548 {
3549 	struct nlookupdata nd;
3550 	int error;
3551 
3552 	error = nlookup_init(&nd, uap->path, UIO_USERSPACE, 0);
3553 	if (error == 0)
3554 		error = kern_chown(&nd, uap->uid, uap->gid);
3555 	nlookup_done(&nd);
3556 	return (error);
3557 }
3558 
3559 /*
3560  * fchown_args(int fd, int uid, int gid)
3561  *
3562  * Set ownership given a file descriptor.
3563  */
3564 int
3565 sys_fchown(struct sysmsg *sysmsg, const struct fchown_args *uap)
3566 {
3567 	struct thread *td = curthread;
3568 	struct proc *p = td->td_proc;
3569 	struct file *fp;
3570 	int error;
3571 
3572 	if ((error = holdvnode(td, uap->fd, &fp)) != 0)
3573 		return (error);
3574 	if (fp->f_nchandle.ncp)
3575 		error = ncp_writechk(&fp->f_nchandle);
3576 	if (error == 0)
3577 		error = setfown(p->p_fd->fd_ncdir.mount,
3578 			(struct vnode *)fp->f_data, uap->uid, uap->gid);
3579 	fdrop(fp);
3580 	return (error);
3581 }
3582 
3583 /*
3584  * fchownat(int fd, char *path, int uid, int gid, int flags)
3585  *
3586  * Set ownership of file pointed to by fd/path.
3587  */
3588 int
3589 sys_fchownat(struct sysmsg *sysmsg, const struct fchownat_args *uap)
3590 {
3591 	struct nlookupdata nd;
3592 	struct file *fp;
3593 	int error;
3594 	int flags;
3595 
3596 	if (uap->flags & ~AT_SYMLINK_NOFOLLOW)
3597 		return (EINVAL);
3598 	flags = (uap->flags & AT_SYMLINK_NOFOLLOW) ? 0 : NLC_FOLLOW;
3599 
3600 	error = nlookup_init_at(&nd, &fp, uap->fd, uap->path,
3601 				UIO_USERSPACE, flags);
3602 	if (error == 0)
3603 		error = kern_chown(&nd, uap->uid, uap->gid);
3604 	nlookup_done_at(&nd, fp);
3605 	return (error);
3606 }
3607 
3608 
3609 static int
3610 getutimes(struct timeval *tvp, struct timespec *tsp)
3611 {
3612 	struct timeval tv[2];
3613 	int error;
3614 
3615 	if (tvp == NULL) {
3616 		microtime(&tv[0]);
3617 		TIMEVAL_TO_TIMESPEC(&tv[0], &tsp[0]);
3618 		tsp[1] = tsp[0];
3619 	} else {
3620 		if ((error = itimerfix(tvp)) != 0)
3621 			return (error);
3622 		TIMEVAL_TO_TIMESPEC(&tvp[0], &tsp[0]);
3623 		TIMEVAL_TO_TIMESPEC(&tvp[1], &tsp[1]);
3624 	}
3625 	return 0;
3626 }
3627 
3628 static int
3629 getutimens(const struct timespec *ts, struct timespec *newts, int *nullflag)
3630 {
3631 	struct timespec tsnow;
3632 	int error;
3633 
3634 	*nullflag = 0;
3635 	nanotime(&tsnow);
3636 	if (ts == NULL) {
3637 		newts[0] = tsnow;
3638 		newts[1] = tsnow;
3639 		*nullflag = 1;
3640 		return (0);
3641 	}
3642 
3643 	newts[0] = ts[0];
3644 	newts[1] = ts[1];
3645 	if (newts[0].tv_nsec == UTIME_OMIT && newts[1].tv_nsec == UTIME_OMIT)
3646 		return (0);
3647 	if (newts[0].tv_nsec == UTIME_NOW && newts[1].tv_nsec == UTIME_NOW)
3648 		*nullflag = 1;
3649 
3650 	if (newts[0].tv_nsec == UTIME_OMIT)
3651 		newts[0].tv_sec = VNOVAL;
3652 	else if (newts[0].tv_nsec == UTIME_NOW)
3653 		newts[0] = tsnow;
3654 	else if ((error = itimespecfix(&newts[0])) != 0)
3655 		return (error);
3656 
3657 	if (newts[1].tv_nsec == UTIME_OMIT)
3658 		newts[1].tv_sec = VNOVAL;
3659 	else if (newts[1].tv_nsec == UTIME_NOW)
3660 		newts[1] = tsnow;
3661 	else if ((error = itimespecfix(&newts[1])) != 0)
3662 		return (error);
3663 
3664 	return (0);
3665 }
3666 
3667 static int
3668 setutimes(struct vnode *vp, struct vattr *vattr,
3669 	  const struct timespec *ts, int nullflag)
3670 {
3671 	struct thread *td = curthread;
3672 	int error;
3673 
3674 	VATTR_NULL(vattr);
3675 	vattr->va_atime = ts[0];
3676 	vattr->va_mtime = ts[1];
3677 	if (nullflag)
3678 		vattr->va_vaflags |= VA_UTIMES_NULL;
3679 	error = VOP_SETATTR(vp, vattr, td->td_ucred);
3680 
3681 	return error;
3682 }
3683 
3684 int
3685 kern_utimes(struct nlookupdata *nd, struct timeval *tptr)
3686 {
3687 	struct timespec ts[2];
3688 	int error;
3689 
3690 	if (tptr) {
3691 		if ((error = getutimes(tptr, ts)) != 0)
3692 			return (error);
3693 	}
3694 	error = kern_utimensat(nd, tptr ? ts : NULL, 0);
3695 	return (error);
3696 }
3697 
3698 /*
3699  * utimes_args(char *path, struct timeval *tptr)
3700  *
3701  * Set the access and modification times of a file.
3702  */
3703 int
3704 sys_utimes(struct sysmsg *sysmsg, const struct utimes_args *uap)
3705 {
3706 	struct timeval tv[2];
3707 	struct nlookupdata nd;
3708 	int error;
3709 
3710 	if (uap->tptr) {
3711  		error = copyin(uap->tptr, tv, sizeof(tv));
3712 		if (error)
3713 			return (error);
3714 	}
3715 	error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
3716 	if (error == 0)
3717 		error = kern_utimes(&nd, uap->tptr ? tv : NULL);
3718 	nlookup_done(&nd);
3719 	return (error);
3720 }
3721 
3722 /*
3723  * lutimes_args(char *path, struct timeval *tptr)
3724  *
3725  * Set the access and modification times of a file.
3726  */
3727 int
3728 sys_lutimes(struct sysmsg *sysmsg, const struct lutimes_args *uap)
3729 {
3730 	struct timeval tv[2];
3731 	struct nlookupdata nd;
3732 	int error;
3733 
3734 	if (uap->tptr) {
3735 		error = copyin(uap->tptr, tv, sizeof(tv));
3736 		if (error)
3737 			return (error);
3738 	}
3739 	error = nlookup_init(&nd, uap->path, UIO_USERSPACE, 0);
3740 	if (error == 0)
3741 		error = kern_utimes(&nd, uap->tptr ? tv : NULL);
3742 	nlookup_done(&nd);
3743 	return (error);
3744 }
3745 
3746 /*
3747  * Set utimes on a file descriptor.  The creds used to open the
3748  * file are used to determine whether the operation is allowed
3749  * or not.
3750  */
3751 int
3752 kern_futimens(int fd, struct timespec *ts)
3753 {
3754 	struct thread *td = curthread;
3755 	struct timespec newts[2];
3756 	struct file *fp;
3757 	struct vnode *vp;
3758 	struct vattr vattr;
3759 	struct vattr_lite lva;
3760 	int nullflag;
3761 	int error;
3762 
3763 	error = getutimens(ts, newts, &nullflag);
3764 	if (error)
3765 		return (error);
3766 	if ((error = holdvnode(td, fd, &fp)) != 0)
3767 		return (error);
3768 	if (fp->f_nchandle.ncp)
3769 		error = ncp_writechk(&fp->f_nchandle);
3770 	if (error == 0) {
3771 		vp = fp->f_data;
3772 		error = vget(vp, LK_EXCLUSIVE);
3773 		if (error == 0) {
3774 			error = VOP_GETATTR_FP(vp, &vattr, fp);
3775 			if (error == 0) {
3776 				lva.va_type = vattr.va_type;
3777 				lva.va_nlink = vattr.va_nlink;
3778 				lva.va_mode = vattr.va_mode;
3779 				lva.va_uid = vattr.va_uid;
3780 				lva.va_gid = vattr.va_gid;
3781 				lva.va_size = vattr.va_size;
3782 				lva.va_flags = vattr.va_flags;
3783 
3784 				error = naccess_lva(&lva, NLC_OWN | NLC_WRITE,
3785 						   fp->f_cred);
3786 			}
3787 			if (error == 0) {
3788 				error = setutimes(vp, &vattr, newts, nullflag);
3789 			}
3790 			vput(vp);
3791 		}
3792 	}
3793 	fdrop(fp);
3794 	return (error);
3795 }
3796 
3797 /*
3798  * futimens_args(int fd, struct timespec *ts)
3799  *
3800  * Set the access and modification times of a file.
3801  */
3802 int
3803 sys_futimens(struct sysmsg *sysmsg, const struct futimens_args *uap)
3804 {
3805 	struct timespec ts[2];
3806 	int error;
3807 
3808 	if (uap->ts) {
3809 		error = copyin(uap->ts, ts, sizeof(ts));
3810 		if (error)
3811 			return (error);
3812 	}
3813 	error = kern_futimens(uap->fd, uap->ts ? ts : NULL);
3814 	return (error);
3815 }
3816 
3817 int
3818 kern_futimes(int fd, struct timeval *tptr)
3819 {
3820 	struct timespec ts[2];
3821 	int error;
3822 
3823 	if (tptr) {
3824 		if ((error = getutimes(tptr, ts)) != 0)
3825 			return (error);
3826 	}
3827 	error = kern_futimens(fd, tptr ? ts : NULL);
3828 	return (error);
3829 }
3830 
3831 /*
3832  * futimes_args(int fd, struct timeval *tptr)
3833  *
3834  * Set the access and modification times of a file.
3835  */
3836 int
3837 sys_futimes(struct sysmsg *sysmsg, const struct futimes_args *uap)
3838 {
3839 	struct timeval tv[2];
3840 	int error;
3841 
3842 	if (uap->tptr) {
3843 		error = copyin(uap->tptr, tv, sizeof(tv));
3844 		if (error)
3845 			return (error);
3846 	}
3847 	error = kern_futimes(uap->fd, uap->tptr ? tv : NULL);
3848 	return (error);
3849 }
3850 
3851 int
3852 kern_utimensat(struct nlookupdata *nd, const struct timespec *ts, int flags)
3853 {
3854 	struct timespec newts[2];
3855 	struct vnode *vp;
3856 	struct vattr vattr;
3857 	int nullflag;
3858 	int error;
3859 
3860 	if (flags & ~AT_SYMLINK_NOFOLLOW)
3861 		return (EINVAL);
3862 
3863 	error = getutimens(ts, newts, &nullflag);
3864 	if (error)
3865 		return (error);
3866 
3867 	nd->nl_flags |= NLC_OWN | NLC_WRITE;
3868 	if ((error = nlookup(nd)) != 0)
3869 		return (error);
3870 	if ((error = ncp_writechk(&nd->nl_nch)) != 0)
3871 		return (error);
3872 	if ((error = cache_vref(&nd->nl_nch, nd->nl_cred, &vp)) != 0)
3873 		return (error);
3874 	if ((error = vn_writechk(vp)) == 0) {
3875 		error = vget(vp, LK_EXCLUSIVE);
3876 		if (error == 0) {
3877 			error = setutimes(vp, &vattr, newts, nullflag);
3878 			vput(vp);
3879 		}
3880 	}
3881 	vrele(vp);
3882 	return (error);
3883 }
3884 
3885 /*
3886  * utimensat_args(int fd, const char *path, const struct timespec *ts, int flags);
3887  *
3888  * Set file access and modification times of a file.
3889  */
3890 int
3891 sys_utimensat(struct sysmsg *sysmsg, const struct utimensat_args *uap)
3892 {
3893 	struct timespec ts[2];
3894 	struct nlookupdata nd;
3895 	struct file *fp;
3896 	int error;
3897 	int flags;
3898 
3899 	if (uap->ts) {
3900 		error = copyin(uap->ts, ts, sizeof(ts));
3901 		if (error)
3902 			return (error);
3903 	}
3904 
3905 	flags = (uap->flags & AT_SYMLINK_NOFOLLOW) ? 0 : NLC_FOLLOW;
3906 	error = nlookup_init_at(&nd, &fp, uap->fd, uap->path,
3907 	                        UIO_USERSPACE, flags);
3908 	if (error == 0)
3909 		error = kern_utimensat(&nd, uap->ts ? ts : NULL, uap->flags);
3910 	nlookup_done_at(&nd, fp);
3911 	return (error);
3912 }
3913 
3914 int
3915 kern_truncate(struct nlookupdata *nd, off_t length)
3916 {
3917 	struct vnode *vp;
3918 	struct vattr vattr;
3919 	int error;
3920 	uid_t uid = 0;
3921 	gid_t gid = 0;
3922 	uint64_t old_size = 0;
3923 
3924 	if (length < 0)
3925 		return(EINVAL);
3926 	nd->nl_flags |= NLC_WRITE | NLC_TRUNCATE;
3927 	if ((error = nlookup(nd)) != 0)
3928 		return (error);
3929 	if ((error = ncp_writechk(&nd->nl_nch)) != 0)
3930 		return (error);
3931 	if ((error = cache_vref(&nd->nl_nch, nd->nl_cred, &vp)) != 0)
3932 		return (error);
3933 	error = vn_lock(vp, LK_EXCLUSIVE | LK_RETRY | LK_FAILRECLAIM);
3934 	if (error) {
3935 		vrele(vp);
3936 		return (error);
3937 	}
3938 	if (vp->v_type == VDIR) {
3939 		error = EISDIR;
3940 		goto done;
3941 	}
3942 	if (vfs_quota_enabled) {
3943 		error = VOP_GETATTR(vp, &vattr);
3944 		KASSERT(error == 0, ("kern_truncate(): VOP_GETATTR didn't return 0"));
3945 		uid = vattr.va_uid;
3946 		gid = vattr.va_gid;
3947 		old_size = vattr.va_size;
3948 	}
3949 
3950 	if ((error = vn_writechk(vp)) == 0) {
3951 		VATTR_NULL(&vattr);
3952 		vattr.va_size = length;
3953 		error = VOP_SETATTR(vp, &vattr, nd->nl_cred);
3954 		VFS_ACCOUNT(nd->nl_nch.mount, uid, gid, length - old_size);
3955 	}
3956 done:
3957 	vput(vp);
3958 	return (error);
3959 }
3960 
3961 /*
3962  * truncate(char *path, int pad, off_t length)
3963  *
3964  * Truncate a file given its path name.
3965  */
3966 int
3967 sys_truncate(struct sysmsg *sysmsg, const struct truncate_args *uap)
3968 {
3969 	struct nlookupdata nd;
3970 	int error;
3971 
3972 	error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
3973 	if (error == 0)
3974 		error = kern_truncate(&nd, uap->length);
3975 	nlookup_done(&nd);
3976 	return error;
3977 }
3978 
3979 int
3980 kern_ftruncate(int fd, off_t length)
3981 {
3982 	struct thread *td = curthread;
3983 	struct vattr vattr;
3984 	struct vnode *vp;
3985 	struct file *fp;
3986 	int error;
3987 	uid_t uid = 0;
3988 	gid_t gid = 0;
3989 	uint64_t old_size = 0;
3990 	struct mount *mp;
3991 
3992 	if (length < 0)
3993 		return(EINVAL);
3994 	if ((error = holdvnode(td, fd, &fp)) != 0)
3995 		return (error);
3996 	if (fp->f_nchandle.ncp) {
3997 		error = ncp_writechk(&fp->f_nchandle);
3998 		if (error)
3999 			goto done;
4000 	}
4001 	if ((fp->f_flag & FWRITE) == 0) {
4002 		error = EINVAL;
4003 		goto done;
4004 	}
4005 	if (fp->f_flag & FAPPENDONLY) {	/* inode was set s/uapnd */
4006 		error = EINVAL;
4007 		goto done;
4008 	}
4009 	vp = (struct vnode *)fp->f_data;
4010 	vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
4011 	if (vp->v_type == VDIR) {
4012 		error = EISDIR;
4013 		vn_unlock(vp);
4014 		goto done;
4015 	}
4016 
4017 	if (vfs_quota_enabled) {
4018 		error = VOP_GETATTR_FP(vp, &vattr, fp);
4019 		KASSERT(error == 0, ("kern_ftruncate(): VOP_GETATTR didn't return 0"));
4020 		uid = vattr.va_uid;
4021 		gid = vattr.va_gid;
4022 		old_size = vattr.va_size;
4023 	}
4024 
4025 	if ((error = vn_writechk(vp)) == 0) {
4026 		VATTR_NULL(&vattr);
4027 		vattr.va_size = length;
4028 		error = VOP_SETATTR_FP(vp, &vattr, fp->f_cred, fp);
4029 		mp = vq_vptomp(vp);
4030 		VFS_ACCOUNT(mp, uid, gid, length - old_size);
4031 	}
4032 	vn_unlock(vp);
4033 done:
4034 	fdrop(fp);
4035 	return (error);
4036 }
4037 
4038 /*
4039  * ftruncate_args(int fd, int pad, off_t length)
4040  *
4041  * Truncate a file given a file descriptor.
4042  */
4043 int
4044 sys_ftruncate(struct sysmsg *sysmsg, const struct ftruncate_args *uap)
4045 {
4046 	int error;
4047 
4048 	error = kern_ftruncate(uap->fd, uap->length);
4049 
4050 	return (error);
4051 }
4052 
4053 int
4054 kern_fsync(int fd, bool fullsync)
4055 {
4056 	struct thread *td = curthread;
4057 	struct vnode *vp;
4058 	struct file *fp;
4059 	vm_object_t obj;
4060 	int error;
4061 
4062 	if ((error = holdvnode(td, fd, &fp)) != 0)
4063 		return (error);
4064 	vp = (struct vnode *)fp->f_data;
4065 	vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
4066 	if ((obj = vp->v_object) != NULL) {
4067 		if (vp->v_mount == NULL ||
4068 		    (vp->v_mount->mnt_kern_flag & MNTK_NOMSYNC) == 0) {
4069 			vm_object_page_clean(obj, 0, 0, 0);
4070 		}
4071 	}
4072 	error = fullsync ?
4073 		VOP_FSYNC_FP(vp, MNT_WAIT, VOP_FSYNC_SYSCALL, fp) :
4074 		VOP_FDATASYNC_FP(vp, MNT_WAIT, VOP_FSYNC_SYSCALL, fp);
4075 	if (error == 0 && vp->v_mount)
4076 		error = buf_fsync(vp);
4077 	vn_unlock(vp);
4078 	fdrop(fp);
4079 
4080 	return (error);
4081 }
4082 
4083 /*
4084  * fsync(int fd)
4085  *
4086  * Sync an open file.
4087  */
4088 int
4089 sys_fsync(struct sysmsg *sysmsg, const struct fsync_args *uap)
4090 {
4091 	return (kern_fsync(uap->fd, true));
4092 }
4093 
4094 /*
4095  * fdatasync(int fd)
4096  *
4097  * Data-sync an open file.
4098  */
4099 int
4100 sys_fdatasync(struct sysmsg *sysmsg, const struct fdatasync_args *uap)
4101 {
4102 	return (kern_fsync(uap->fd, false));
4103 }
4104 
4105 /*
4106  * rename op.
4107  *
4108  * NOTE: error == 0 and nl_dvp is NULL indicates a mount point, operation
4109  *	 disallowed.  e.g. /var/cache where /var/cache is a null-mount, for
4110  *	 example.
4111  */
4112 int
4113 kern_rename(struct nlookupdata *fromnd, struct nlookupdata *tond)
4114 {
4115 	struct nchandle fnchd;
4116 	struct nchandle tnchd;
4117 	struct namecache *ncp;
4118 	struct vnode *fdvp;
4119 	struct vnode *tdvp;
4120 	struct mount *mp;
4121 	struct mount *userenlk;
4122 	int error;
4123 	u_int fncp_gen;
4124 	u_int tncp_gen;
4125 
4126 	bwillinode(1);
4127 	fromnd->nl_flags |= NLC_REFDVP | NLC_RENAME_SRC;
4128 	if ((error = nlookup(fromnd)) != 0)
4129 		return (error);
4130 
4131 	/*
4132 	 * Attempt to rename a mount point (from or to)
4133 	 */
4134 	if (error == 0 && fromnd->nl_dvp == NULL)
4135 		return (EINVAL);
4136 
4137 	if ((fnchd.ncp = fromnd->nl_nch.ncp->nc_parent) == NULL)
4138 		return (ENOENT);
4139 	fnchd.mount = fromnd->nl_nch.mount;
4140 	cache_hold(&fnchd);
4141 
4142 	/*
4143 	 * unlock the source nch so we can lookup the target nch without
4144 	 * deadlocking.  The target may or may not exist so we do not check
4145 	 * for a target vp like kern_mkdir() and other creation functions do.
4146 	 *
4147 	 * The source and target directories are ref'd and rechecked after
4148 	 * everything is relocked to determine if the source or target file
4149 	 * has been renamed.
4150 	 */
4151 	KKASSERT(fromnd->nl_flags & NLC_NCPISLOCKED);
4152 	fromnd->nl_flags &= ~NLC_NCPISLOCKED;
4153 	fncp_gen = fromnd->nl_nch.ncp->nc_generation;
4154 
4155 	if (fromnd->nl_nch.ncp->nc_vp &&
4156 	    fromnd->nl_nch.ncp->nc_vp->v_type == VDIR) {
4157 		userenlk = fnchd.mount;
4158 		cache_unlock(&fromnd->nl_nch);
4159 		lockmgr(&userenlk->mnt_renlock, LK_EXCLUSIVE);
4160 	} else {
4161 		userenlk = NULL;
4162 		cache_unlock(&fromnd->nl_nch);
4163 	}
4164 
4165 	/*
4166 	 * Lookup target
4167 	 */
4168 	tond->nl_flags |= NLC_RENAME_DST | NLC_REFDVP;
4169 	if ((error = nlookup(tond)) != 0) {
4170 		cache_drop(&fnchd);
4171 		goto done;
4172 	}
4173 	tncp_gen = tond->nl_nch.ncp->nc_generation;
4174 
4175 	/*
4176 	 * Attempt to rename a mount point (from or to)
4177 	 */
4178 	if (error == 0 && tond->nl_dvp == NULL) {
4179 		cache_drop(&fnchd);
4180 		error = ENOENT;
4181 		goto done;
4182 	}
4183 
4184 	if ((tnchd.ncp = tond->nl_nch.ncp->nc_parent) == NULL) {
4185 		cache_drop(&fnchd);
4186 		error = ENOENT;
4187 		goto done;
4188 	}
4189 	tnchd.mount = tond->nl_nch.mount;
4190 	cache_hold(&tnchd);
4191 
4192 	/*
4193 	 * If the source and target are the same there is nothing to do
4194 	 */
4195 	if (fromnd->nl_nch.ncp == tond->nl_nch.ncp) {
4196 		cache_drop(&fnchd);
4197 		cache_drop(&tnchd);
4198 		error = 0;
4199 		goto done;
4200 	}
4201 
4202 	/*
4203 	 * Mount points cannot be renamed or overwritten
4204 	 */
4205 	if ((fromnd->nl_nch.ncp->nc_flag | tond->nl_nch.ncp->nc_flag) &
4206 	    NCF_ISMOUNTPT
4207 	) {
4208 		cache_drop(&fnchd);
4209 		cache_drop(&tnchd);
4210 		error = EINVAL;
4211 		goto done;
4212 	}
4213 
4214 	/*
4215 	 * Lock all four namecache entries.  tond is already locked.
4216 	 */
4217 	cache_lock4_tondlocked(&fnchd, &fromnd->nl_nch,
4218 			       &tnchd, &tond->nl_nch,
4219 			       fromnd->nl_cred, tond->nl_cred);
4220 	fromnd->nl_flags |= NLC_NCPISLOCKED;
4221 
4222 	/*
4223 	 * If the namecache generation changed for either fromnd or tond,
4224 	 * we must retry.
4225 	 */
4226 	if (fromnd->nl_nch.ncp->nc_generation != fncp_gen ||
4227 	    tond->nl_nch.ncp->nc_generation != tncp_gen) {
4228 		krateprintf(&krate_rename,
4229 			"kern_rename: retry due to race on: "
4230 			"\"%s\" -> \"%s\"\n",
4231 			fromnd->nl_nch.ncp->nc_name,
4232 			tond->nl_nch.ncp->nc_name);
4233 		error = EAGAIN;
4234 		goto finish;
4235 	}
4236 
4237 	/*
4238 	 * If either fromnd or tond are marked destroyed a ripout occured
4239 	 * out from under us and we must retry.
4240 	 */
4241 	if ((fromnd->nl_nch.ncp->nc_flag & (NCF_DESTROYED | NCF_UNRESOLVED)) ||
4242 	    fromnd->nl_nch.ncp->nc_vp == NULL ||
4243 	    (tond->nl_nch.ncp->nc_flag & (NCF_DESTROYED | NCF_UNRESOLVED))) {
4244 		krateprintf(&krate_rename,
4245 			"kern_rename: retry due to ripout on: "
4246 			"\"%s\" -> \"%s\"\n",
4247 			fromnd->nl_nch.ncp->nc_name,
4248 			tond->nl_nch.ncp->nc_name);
4249 		error = EAGAIN;
4250 		goto finish;
4251 	}
4252 
4253 	/*
4254 	 * Make sure the parent directories linkages are the same.  We have
4255 	 * already checked that fromnd and tond are not mount points so this
4256 	 * should not loop forever on a cross-mount.
4257 	 */
4258 	if (fnchd.ncp != fromnd->nl_nch.ncp->nc_parent ||
4259 	    tnchd.ncp != tond->nl_nch.ncp->nc_parent) {
4260 		error = EAGAIN;
4261 		goto finish;
4262 	}
4263 
4264 	/*
4265 	 * Both the source and target must be within the same filesystem and
4266 	 * in the same filesystem as their parent directories within the
4267 	 * namecache topology.
4268 	 *
4269 	 * NOTE: fromnd's nc_mount or nc_vp could be NULL.
4270 	 */
4271 	mp = fnchd.mount;
4272 	if (mp != tnchd.mount || mp != fromnd->nl_nch.mount ||
4273 	    mp != tond->nl_nch.mount) {
4274 		error = EXDEV;
4275 		goto finish;
4276 	}
4277 
4278 	/*
4279 	 * Make sure the mount point is writable
4280 	 */
4281 	if ((error = ncp_writechk(&tond->nl_nch)) != 0) {
4282 		goto finish;
4283 	}
4284 
4285 	/*
4286 	 * If the target exists and either the source or target is a directory,
4287 	 * then both must be directories.
4288 	 *
4289 	 * Due to relocking of the source, fromnd->nl_nch.ncp->nc_vp might h
4290 	 * have become NULL.
4291 	 */
4292 	if (tond->nl_nch.ncp->nc_vp) {
4293 		if (fromnd->nl_nch.ncp->nc_vp == NULL) {
4294 			error = ENOENT;
4295 		} else if (fromnd->nl_nch.ncp->nc_vp->v_type == VDIR) {
4296 			if (tond->nl_nch.ncp->nc_vp->v_type != VDIR)
4297 				error = ENOTDIR;
4298 		} else if (tond->nl_nch.ncp->nc_vp->v_type == VDIR) {
4299 			error = EISDIR;
4300 		}
4301 	}
4302 
4303 	/*
4304 	 * You cannot rename a source into itself or a subdirectory of itself.
4305 	 * We check this by travsersing the target directory upwards looking
4306 	 * for a match against the source.
4307 	 *
4308 	 * Only required when renaming a directory, in which case userenlk is
4309 	 * non-NULL.
4310 	 */
4311 	if (__predict_false(userenlk && error == 0)) {
4312 		for (ncp = tnchd.ncp; ncp; ncp = ncp->nc_parent) {
4313 			if (fromnd->nl_nch.ncp == ncp) {
4314 				error = EINVAL;
4315 				break;
4316 			}
4317 		}
4318 	}
4319 
4320 	/*
4321 	 * Even though the namespaces are different, they may still represent
4322 	 * hardlinks to the same file.  The filesystem might have a hard time
4323 	 * with this so we issue a NREMOVE of the source instead of a NRENAME
4324 	 * when we detect the situation.
4325 	 */
4326 	if (error == 0) {
4327 		fdvp = fromnd->nl_dvp;
4328 		tdvp = tond->nl_dvp;
4329 		if (fdvp == NULL || tdvp == NULL) {
4330 			error = EPERM;
4331 		} else if (fromnd->nl_nch.ncp->nc_vp == tond->nl_nch.ncp->nc_vp) {
4332 			error = VOP_NREMOVE(&fromnd->nl_nch, fdvp,
4333 					    fromnd->nl_cred);
4334 		} else {
4335 			error = VOP_NRENAME(&fromnd->nl_nch, &tond->nl_nch,
4336 					    fdvp, tdvp, tond->nl_cred);
4337 		}
4338 	}
4339 finish:
4340 	cache_put(&tnchd);
4341 	cache_put(&fnchd);
4342 done:
4343 	if (userenlk)
4344 		lockmgr(&userenlk->mnt_renlock, LK_RELEASE);
4345 	return (error);
4346 }
4347 
4348 /*
4349  * rename_args(char *from, char *to)
4350  *
4351  * Rename files.  Source and destination must either both be directories,
4352  * or both not be directories.  If target is a directory, it must be empty.
4353  */
4354 int
4355 sys_rename(struct sysmsg *sysmsg, const struct rename_args *uap)
4356 {
4357 	struct nlookupdata fromnd, tond;
4358 	int error;
4359 
4360 	do {
4361 		error = nlookup_init(&fromnd, uap->from, UIO_USERSPACE, 0);
4362 		if (error == 0) {
4363 			error = nlookup_init(&tond, uap->to, UIO_USERSPACE, 0);
4364 			if (error == 0)
4365 				error = kern_rename(&fromnd, &tond);
4366 			nlookup_done(&tond);
4367 		}
4368 		nlookup_done(&fromnd);
4369 	} while (error == EAGAIN);
4370 	return (error);
4371 }
4372 
4373 /*
4374  * renameat_args(int oldfd, char *old, int newfd, char *new)
4375  *
4376  * Rename files using paths relative to the directories associated with
4377  * oldfd and newfd.  Source and destination must either both be directories,
4378  * or both not be directories.  If target is a directory, it must be empty.
4379  */
4380 int
4381 sys_renameat(struct sysmsg *sysmsg, const struct renameat_args *uap)
4382 {
4383 	struct nlookupdata oldnd, newnd;
4384 	struct file *oldfp, *newfp;
4385 	int error;
4386 
4387 	do {
4388 		error = nlookup_init_at(&oldnd, &oldfp,
4389 					uap->oldfd, uap->old,
4390 					UIO_USERSPACE, 0);
4391 		if (error == 0) {
4392 			error = nlookup_init_at(&newnd, &newfp,
4393 						uap->newfd, uap->new,
4394 						UIO_USERSPACE, 0);
4395 			if (error == 0)
4396 				error = kern_rename(&oldnd, &newnd);
4397 			nlookup_done_at(&newnd, newfp);
4398 		}
4399 		nlookup_done_at(&oldnd, oldfp);
4400 	} while (error == EAGAIN);
4401 	return (error);
4402 }
4403 
4404 int
4405 kern_mkdir(struct nlookupdata *nd, int mode)
4406 {
4407 	struct thread *td = curthread;
4408 	struct proc *p = td->td_proc;
4409 	struct vnode *vp;
4410 	struct vattr vattr;
4411 	int error;
4412 
4413 	bwillinode(1);
4414 	nd->nl_flags |= NLC_WILLBEDIR | NLC_CREATE | NLC_REFDVP;
4415 	if ((error = nlookup(nd)) != 0)
4416 		return (error);
4417 
4418 	if (nd->nl_nch.ncp->nc_vp)
4419 		return (EEXIST);
4420 	if (nd->nl_dvp == NULL)
4421 		return (EINVAL);
4422 	if ((error = ncp_writechk(&nd->nl_nch)) != 0)
4423 		return (error);
4424 	VATTR_NULL(&vattr);
4425 	vattr.va_type = VDIR;
4426 	vattr.va_mode = (mode & ACCESSPERMS) &~ p->p_fd->fd_cmask;
4427 
4428 	vp = NULL;
4429 	error = VOP_NMKDIR(&nd->nl_nch, nd->nl_dvp, &vp, td->td_ucred, &vattr);
4430 	if (error == 0)
4431 		vput(vp);
4432 	return (error);
4433 }
4434 
4435 /*
4436  * mkdir_args(char *path, int mode)
4437  *
4438  * Make a directory file.
4439  */
4440 int
4441 sys_mkdir(struct sysmsg *sysmsg, const struct mkdir_args *uap)
4442 {
4443 	struct nlookupdata nd;
4444 	int error;
4445 
4446 	error = nlookup_init(&nd, uap->path, UIO_USERSPACE, 0);
4447 	if (error == 0)
4448 		error = kern_mkdir(&nd, uap->mode);
4449 	nlookup_done(&nd);
4450 	return (error);
4451 }
4452 
4453 /*
4454  * mkdirat_args(int fd, char *path, mode_t mode)
4455  *
4456  * Make a directory file.  The path is relative to the directory associated
4457  * with fd.
4458  */
4459 int
4460 sys_mkdirat(struct sysmsg *sysmsg, const struct mkdirat_args *uap)
4461 {
4462 	struct nlookupdata nd;
4463 	struct file *fp;
4464 	int error;
4465 
4466 	error = nlookup_init_at(&nd, &fp, uap->fd, uap->path, UIO_USERSPACE, 0);
4467 	if (error == 0)
4468 		error = kern_mkdir(&nd, uap->mode);
4469 	nlookup_done_at(&nd, fp);
4470 	return (error);
4471 }
4472 
4473 int
4474 kern_rmdir(struct nlookupdata *nd)
4475 {
4476 	int error;
4477 
4478 	bwillinode(1);
4479 	nd->nl_flags |= NLC_DELETE | NLC_REFDVP;
4480 	if ((error = nlookup(nd)) != 0)
4481 		return (error);
4482 
4483 	/*
4484 	 * Do not allow directories representing mount points to be
4485 	 * deleted, even if empty.  Check write perms on mount point
4486 	 * in case the vnode is aliased (aka nullfs).
4487 	 */
4488 	if (nd->nl_nch.ncp->nc_flag & (NCF_ISMOUNTPT))
4489 		return (EBUSY);
4490 	if (nd->nl_dvp == NULL)
4491 		return (EINVAL);
4492 	if ((error = ncp_writechk(&nd->nl_nch)) != 0)
4493 		return (error);
4494 	error = VOP_NRMDIR(&nd->nl_nch, nd->nl_dvp, nd->nl_cred);
4495 	return (error);
4496 }
4497 
4498 /*
4499  * rmdir_args(char *path)
4500  *
4501  * Remove a directory file.
4502  */
4503 int
4504 sys_rmdir(struct sysmsg *sysmsg, const struct rmdir_args *uap)
4505 {
4506 	struct nlookupdata nd;
4507 	int error;
4508 
4509 	error = nlookup_init(&nd, uap->path, UIO_USERSPACE, 0);
4510 	if (error == 0)
4511 		error = kern_rmdir(&nd);
4512 	nlookup_done(&nd);
4513 	return (error);
4514 }
4515 
4516 int
4517 kern_getdirentries(int fd, char *buf, u_int count, long *basep, int *res,
4518 		   enum uio_seg direction)
4519 {
4520 	struct thread *td = curthread;
4521 	struct vnode *vp;
4522 	struct file *fp;
4523 	struct uio auio;
4524 	struct iovec aiov;
4525 	off_t loff;
4526 	int error, eofflag;
4527 
4528 	if ((error = holdvnode(td, fd, &fp)) != 0)
4529 		return (error);
4530 	if ((fp->f_flag & FREAD) == 0) {
4531 		error = EBADF;
4532 		goto done;
4533 	}
4534 	vp = (struct vnode *)fp->f_data;
4535 	if (vp->v_type != VDIR) {
4536 		error = EINVAL;
4537 		goto done;
4538 	}
4539 	aiov.iov_base = buf;
4540 	aiov.iov_len = count;
4541 	auio.uio_iov = &aiov;
4542 	auio.uio_iovcnt = 1;
4543 	auio.uio_rw = UIO_READ;
4544 	auio.uio_segflg = direction;
4545 	auio.uio_td = td;
4546 	auio.uio_resid = count;
4547 	loff = auio.uio_offset = fp->f_offset;
4548 	error = VOP_READDIR_FP(vp, &auio, fp->f_cred, &eofflag, NULL, NULL, fp);
4549 	fp->f_offset = auio.uio_offset;
4550 	if (error)
4551 		goto done;
4552 
4553 	/*
4554 	 * WARNING!  *basep may not be wide enough to accomodate the
4555 	 * seek offset.   XXX should we hack this to return the upper 32 bits
4556 	 * for offsets greater then 4G?
4557 	 */
4558 	if (basep) {
4559 		*basep = (long)loff;
4560 	}
4561 	*res = count - auio.uio_resid;
4562 done:
4563 	fdrop(fp);
4564 	return (error);
4565 }
4566 
4567 /*
4568  * getdirentries_args(int fd, char *buf, u_int conut, long *basep)
4569  *
4570  * Read a block of directory entries in a file system independent format.
4571  */
4572 int
4573 sys_getdirentries(struct sysmsg *sysmsg, const struct getdirentries_args *uap)
4574 {
4575 	long base;
4576 	int error;
4577 
4578 	error = kern_getdirentries(uap->fd, uap->buf, uap->count, &base,
4579 				   &sysmsg->sysmsg_result, UIO_USERSPACE);
4580 
4581 	if (error == 0 && uap->basep)
4582 		error = copyout(&base, uap->basep, sizeof(*uap->basep));
4583 	return (error);
4584 }
4585 
4586 /*
4587  * getdents_args(int fd, char *buf, size_t count)
4588  */
4589 int
4590 sys_getdents(struct sysmsg *sysmsg, const struct getdents_args *uap)
4591 {
4592 	int error;
4593 
4594 	error = kern_getdirentries(uap->fd, uap->buf, uap->count, NULL,
4595 				   &sysmsg->sysmsg_result, UIO_USERSPACE);
4596 
4597 	return (error);
4598 }
4599 
4600 /*
4601  * Set the mode mask for creation of filesystem nodes.
4602  *
4603  * umask(int newmask)
4604  */
4605 int
4606 sys_umask(struct sysmsg *sysmsg, const struct umask_args *uap)
4607 {
4608 	struct thread *td = curthread;
4609 	struct proc *p = td->td_proc;
4610 	struct filedesc *fdp;
4611 
4612 	fdp = p->p_fd;
4613 	sysmsg->sysmsg_result = fdp->fd_cmask;
4614 	fdp->fd_cmask = uap->newmask & ALLPERMS;
4615 	return (0);
4616 }
4617 
4618 /*
4619  * revoke(char *path)
4620  *
4621  * Void all references to file by ripping underlying filesystem
4622  * away from vnode.
4623  */
4624 int
4625 sys_revoke(struct sysmsg *sysmsg, const struct revoke_args *uap)
4626 {
4627 	struct nlookupdata nd;
4628 	struct vattr vattr;
4629 	struct vnode *vp;
4630 	struct ucred *cred;
4631 	int error;
4632 
4633 	vp = NULL;
4634 	error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
4635 	if (error == 0)
4636 		error = nlookup(&nd);
4637 	if (error == 0)
4638 		error = cache_vref(&nd.nl_nch, nd.nl_cred, &vp);
4639 	cred = crhold(nd.nl_cred);
4640 	nlookup_done(&nd);
4641 	if (error == 0) {
4642 		if (error == 0)
4643 			error = VOP_GETATTR(vp, &vattr);
4644 		if (error == 0 && cred->cr_uid != vattr.va_uid)
4645 			error = priv_check_cred(cred, PRIV_VFS_REVOKE, 0);
4646 		if (error == 0 && (vp->v_type == VCHR || vp->v_type == VBLK)) {
4647 			if (vcount(vp) > 0)
4648 				error = vrevoke(vp, cred);
4649 		} else if (error == 0) {
4650 			error = vrevoke(vp, cred);
4651 		}
4652 		vrele(vp);
4653 	}
4654 	if (cred)
4655 		crfree(cred);
4656 	return (error);
4657 }
4658 
4659 /*
4660  * getfh_args(char *fname, fhandle_t *fhp)
4661  *
4662  * Get (NFS) file handle
4663  *
4664  * NOTE: We use the fsid of the covering mount, even if it is a nullfs
4665  * mount.  This allows nullfs mounts to be explicitly exported.
4666  *
4667  * WARNING: nullfs mounts of HAMMER PFS ROOTs are safe.
4668  *
4669  * 	    nullfs mounts of subdirectories are not safe.  That is, it will
4670  *	    work, but you do not really have protection against access to
4671  *	    the related parent directories.
4672  */
4673 int
4674 sys_getfh(struct sysmsg *sysmsg, const struct getfh_args *uap)
4675 {
4676 	struct thread *td = curthread;
4677 	struct nlookupdata nd;
4678 	fhandle_t fh;
4679 	struct vnode *vp;
4680 	struct mount *mp;
4681 	int error;
4682 
4683 	/*
4684 	 * Must be super user
4685 	 */
4686 	if ((error = priv_check(td, PRIV_ROOT)) != 0)
4687 		return (error);
4688 
4689 	vp = NULL;
4690 	error = nlookup_init(&nd, uap->fname, UIO_USERSPACE, NLC_FOLLOW);
4691 	if (error == 0)
4692 		error = nlookup(&nd);
4693 	if (error == 0)
4694 		error = cache_vget(&nd.nl_nch, nd.nl_cred, LK_EXCLUSIVE, &vp);
4695 	mp = nd.nl_nch.mount;
4696 	nlookup_done(&nd);
4697 	if (error == 0) {
4698 		bzero(&fh, sizeof(fh));
4699 		fh.fh_fsid = mp->mnt_stat.f_fsid;
4700 		error = VFS_VPTOFH(vp, &fh.fh_fid);
4701 		vput(vp);
4702 		if (error == 0)
4703 			error = copyout(&fh, uap->fhp, sizeof(fh));
4704 	}
4705 	return (error);
4706 }
4707 
4708 /*
4709  * fhopen_args(const struct fhandle *u_fhp, int flags)
4710  *
4711  * syscall for the rpc.lockd to use to translate a NFS file handle into
4712  * an open descriptor.
4713  *
4714  * warning: do not remove the priv_check() call or this becomes one giant
4715  * security hole.
4716  */
4717 int
4718 sys_fhopen(struct sysmsg *sysmsg, const struct fhopen_args *uap)
4719 {
4720 	struct thread *td = curthread;
4721 	struct filedesc *fdp = td->td_proc->p_fd;
4722 	struct mount *mp;
4723 	struct vnode *vp;
4724 	struct fhandle fhp;
4725 	struct vattr vat;
4726 	struct vattr *vap = &vat;
4727 	struct flock lf;
4728 	int fmode, mode, error = 0, type;
4729 	struct file *nfp;
4730 	struct file *fp;
4731 	int indx;
4732 
4733 	/*
4734 	 * Must be super user
4735 	 */
4736 	error = priv_check(td, PRIV_ROOT);
4737 	if (error)
4738 		return (error);
4739 
4740 	fmode = FFLAGS(uap->flags);
4741 
4742 	/*
4743 	 * Why not allow a non-read/write open for our lockd?
4744 	 */
4745 	if (((fmode & (FREAD | FWRITE)) == 0) || (fmode & O_CREAT))
4746 		return (EINVAL);
4747 	error = copyin(uap->u_fhp, &fhp, sizeof(fhp));
4748 	if (error)
4749 		return(error);
4750 
4751 	/*
4752 	 * Find the mount point
4753 	 */
4754 	mp = vfs_getvfs(&fhp.fh_fsid);
4755 	if (mp == NULL) {
4756 		error = ESTALE;
4757 		goto done2;
4758 	}
4759 	/* now give me my vnode, it gets returned to me locked */
4760 	error = VFS_FHTOVP(mp, NULL, &fhp.fh_fid, &vp);
4761 	if (error)
4762 		goto done;
4763  	/*
4764 	 * from now on we have to make sure not
4765 	 * to forget about the vnode
4766 	 * any error that causes an abort must vput(vp)
4767 	 * just set error = err and 'goto bad;'.
4768 	 */
4769 
4770 	/*
4771 	 * from vn_open
4772 	 */
4773 	if (vp->v_type == VLNK) {
4774 		error = EMLINK;
4775 		goto bad;
4776 	}
4777 	if (vp->v_type == VSOCK) {
4778 		error = EOPNOTSUPP;
4779 		goto bad;
4780 	}
4781 	mode = 0;
4782 	if (fmode & (FWRITE | O_TRUNC)) {
4783 		if (vp->v_type == VDIR) {
4784 			error = EISDIR;
4785 			goto bad;
4786 		}
4787 		error = vn_writechk(vp);
4788 		if (error)
4789 			goto bad;
4790 		mode |= VWRITE;
4791 	}
4792 	if (fmode & FREAD)
4793 		mode |= VREAD;
4794 	if (mode) {
4795 		error = VOP_ACCESS(vp, mode, td->td_ucred);
4796 		if (error)
4797 			goto bad;
4798 	}
4799 	if (fmode & O_TRUNC) {
4800 		vn_unlock(vp);				/* XXX */
4801 		vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);	/* XXX */
4802 		VATTR_NULL(vap);
4803 		vap->va_size = 0;
4804 		error = VOP_SETATTR(vp, vap, td->td_ucred);
4805 		if (error)
4806 			goto bad;
4807 	}
4808 
4809 	/*
4810 	 * VOP_OPEN needs the file pointer so it can potentially override
4811 	 * it.
4812 	 *
4813 	 * WARNING! no f_nchandle will be associated when fhopen()ing a
4814 	 * directory.  XXX
4815 	 */
4816 	if ((error = falloc(td->td_lwp, &nfp, &indx)) != 0)
4817 		goto bad;
4818 	error = VOP_OPEN(vp, fmode, td->td_ucred, &nfp);
4819 	fp = nfp;
4820 
4821 	if (error) {
4822 		/*
4823 		 * setting f_ops this way prevents VOP_CLOSE from being
4824 		 * called or fdrop() releasing the vp from v_data.   Since
4825 		 * the VOP_OPEN failed we don't want to VOP_CLOSE.
4826 		 */
4827 		fp->f_ops = &badfileops;
4828 		fp->f_data = NULL;
4829 		goto bad_drop;
4830 	}
4831 
4832 	/*
4833 	 * The fp is given its own reference, we still have our ref and lock.
4834 	 *
4835 	 * Assert that all regular files must be created with a VM object.
4836 	 */
4837 	if (vp->v_type == VREG && vp->v_object == NULL) {
4838 		kprintf("fhopen: regular file did not "
4839 			"have VM object: %p\n",
4840 			vp);
4841 		goto bad_drop;
4842 	}
4843 
4844 	/*
4845 	 * The open was successful.  Handle any locking requirements.
4846 	 */
4847 	if (fmode & (O_EXLOCK | O_SHLOCK)) {
4848 		lf.l_whence = SEEK_SET;
4849 		lf.l_start = 0;
4850 		lf.l_len = 0;
4851 		if (fmode & O_EXLOCK)
4852 			lf.l_type = F_WRLCK;
4853 		else
4854 			lf.l_type = F_RDLCK;
4855 		if (fmode & FNONBLOCK)
4856 			type = 0;
4857 		else
4858 			type = F_WAIT;
4859 		vn_unlock(vp);
4860 		if ((error = VOP_ADVLOCK(vp, (caddr_t)fp, F_SETLK,
4861 					 &lf, type)) != 0) {
4862 			/*
4863 			 * release our private reference.
4864 			 */
4865 			fsetfd(fdp, NULL, indx);
4866 			fdrop(fp);
4867 			vrele(vp);
4868 			goto done;
4869 		}
4870 		vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
4871 		atomic_set_int(&fp->f_flag, FHASLOCK);	/* race ok */
4872 	}
4873 
4874 	/*
4875 	 * Clean up.  Associate the file pointer with the previously
4876 	 * reserved descriptor and return it.
4877 	 */
4878 	vput(vp);
4879 	if (uap->flags & O_CLOEXEC)
4880 		fdp->fd_files[indx].fileflags |= UF_EXCLOSE;
4881 	fsetfd(fdp, fp, indx);
4882 	fdrop(fp);
4883 	sysmsg->sysmsg_result = indx;
4884 	mount_drop(mp);
4885 
4886 	return (error);
4887 
4888 bad_drop:
4889 	fsetfd(fdp, NULL, indx);
4890 	fdrop(fp);
4891 bad:
4892 	vput(vp);
4893 done:
4894 	mount_drop(mp);
4895 done2:
4896 	return (error);
4897 }
4898 
4899 /*
4900  * fhstat_args(struct fhandle *u_fhp, struct stat *sb)
4901  */
4902 int
4903 sys_fhstat(struct sysmsg *sysmsg, const struct fhstat_args *uap)
4904 {
4905 	struct thread *td = curthread;
4906 	struct stat sb;
4907 	fhandle_t fh;
4908 	struct mount *mp;
4909 	struct vnode *vp;
4910 	int error;
4911 
4912 	/*
4913 	 * Must be super user
4914 	 */
4915 	error = priv_check(td, PRIV_ROOT);
4916 	if (error)
4917 		return (error);
4918 
4919 	error = copyin(uap->u_fhp, &fh, sizeof(fhandle_t));
4920 	if (error)
4921 		return (error);
4922 
4923 	if ((mp = vfs_getvfs(&fh.fh_fsid)) == NULL)
4924 		error = ESTALE;
4925 	if (error == 0) {
4926 		if ((error = VFS_FHTOVP(mp, NULL, &fh.fh_fid, &vp)) == 0) {
4927 			error = vn_stat(vp, &sb, td->td_ucred);
4928 			vput(vp);
4929 		}
4930 	}
4931 	if (error == 0)
4932 		error = copyout(&sb, uap->sb, sizeof(sb));
4933 	if (mp)
4934 		mount_drop(mp);
4935 
4936 	return (error);
4937 }
4938 
4939 /*
4940  * fhstatfs_args(struct fhandle *u_fhp, struct statfs *buf)
4941  */
4942 int
4943 sys_fhstatfs(struct sysmsg *sysmsg, const struct fhstatfs_args *uap)
4944 {
4945 	struct thread *td = curthread;
4946 	struct proc *p = td->td_proc;
4947 	struct statfs *sp;
4948 	struct mount *mp;
4949 	struct vnode *vp;
4950 	struct statfs sb;
4951 	char *fullpath, *freepath;
4952 	fhandle_t fh;
4953 	int error;
4954 
4955 	/*
4956 	 * Must be super user
4957 	 */
4958 	if ((error = priv_check(td, PRIV_ROOT)))
4959 		return (error);
4960 
4961 	if ((error = copyin(uap->u_fhp, &fh, sizeof(fhandle_t))) != 0)
4962 		return (error);
4963 
4964 	if ((mp = vfs_getvfs(&fh.fh_fsid)) == NULL) {
4965 		error = ESTALE;
4966 		goto done;
4967 	}
4968 	if (p != NULL && !chroot_visible_mnt(mp, p)) {
4969 		error = ESTALE;
4970 		goto done;
4971 	}
4972 
4973 	if ((error = VFS_FHTOVP(mp, NULL, &fh.fh_fid, &vp)) != 0)
4974 		goto done;
4975 	mp = vp->v_mount;
4976 	sp = &mp->mnt_stat;
4977 	vput(vp);
4978 	if ((error = VFS_STATFS(mp, sp, td->td_ucred)) != 0)
4979 		goto done;
4980 
4981 	error = mount_path(p, mp, &fullpath, &freepath);
4982 	if (error)
4983 		goto done;
4984 	bzero(sp->f_mntonname, sizeof(sp->f_mntonname));
4985 	strlcpy(sp->f_mntonname, fullpath, sizeof(sp->f_mntonname));
4986 	kfree(freepath, M_TEMP);
4987 
4988 	sp->f_flags = mp->mnt_flag & MNT_VISFLAGMASK;
4989 	if (priv_check(td, PRIV_ROOT)) {
4990 		bcopy(sp, &sb, sizeof(sb));
4991 		sb.f_fsid.val[0] = sb.f_fsid.val[1] = 0;
4992 		sp = &sb;
4993 	}
4994 	error = copyout(sp, uap->buf, sizeof(*sp));
4995 done:
4996 	if (mp)
4997 		mount_drop(mp);
4998 
4999 	return (error);
5000 }
5001 
5002 /*
5003  * fhstatvfs_args(struct fhandle *u_fhp, struct statvfs *buf)
5004  */
5005 int
5006 sys_fhstatvfs(struct sysmsg *sysmsg, const struct fhstatvfs_args *uap)
5007 {
5008 	struct thread *td = curthread;
5009 	struct proc *p = td->td_proc;
5010 	struct statvfs *sp;
5011 	struct mount *mp;
5012 	struct vnode *vp;
5013 	fhandle_t fh;
5014 	int error;
5015 
5016 	/*
5017 	 * Must be super user
5018 	 */
5019 	if ((error = priv_check(td, PRIV_ROOT)))
5020 		return (error);
5021 
5022 	if ((error = copyin(uap->u_fhp, &fh, sizeof(fhandle_t))) != 0)
5023 		return (error);
5024 
5025 	if ((mp = vfs_getvfs(&fh.fh_fsid)) == NULL) {
5026 		error = ESTALE;
5027 		goto done;
5028 	}
5029 	if (p != NULL && !chroot_visible_mnt(mp, p)) {
5030 		error = ESTALE;
5031 		goto done;
5032 	}
5033 
5034 	if ((error = VFS_FHTOVP(mp, NULL, &fh.fh_fid, &vp)))
5035 		goto done;
5036 	mp = vp->v_mount;
5037 	sp = &mp->mnt_vstat;
5038 	vput(vp);
5039 	if ((error = VFS_STATVFS(mp, sp, td->td_ucred)) != 0)
5040 		goto done;
5041 
5042 	sp->f_flag = 0;
5043 	if (mp->mnt_flag & MNT_RDONLY)
5044 		sp->f_flag |= ST_RDONLY;
5045 	if (mp->mnt_flag & MNT_NOSUID)
5046 		sp->f_flag |= ST_NOSUID;
5047 	error = copyout(sp, uap->buf, sizeof(*sp));
5048 done:
5049 	if (mp)
5050 		mount_drop(mp);
5051 	return (error);
5052 }
5053 
5054 
5055 /*
5056  * Syscall to push extended attribute configuration information into the
5057  * VFS.  Accepts a path, which it converts to a mountpoint, as well as
5058  * a command (int cmd), and attribute name and misc data.  For now, the
5059  * attribute name is left in userspace for consumption by the VFS_op.
5060  * It will probably be changed to be copied into sysspace by the
5061  * syscall in the future, once issues with various consumers of the
5062  * attribute code have raised their hands.
5063  *
5064  * Currently this is used only by UFS Extended Attributes.
5065  */
5066 int
5067 sys_extattrctl(struct sysmsg *sysmsg, const struct extattrctl_args *uap)
5068 {
5069 	struct nlookupdata nd;
5070 	struct vnode *vp;
5071 	char attrname[EXTATTR_MAXNAMELEN];
5072 	int error;
5073 	size_t size;
5074 
5075 	attrname[0] = 0;
5076 	vp = NULL;
5077 	error = 0;
5078 
5079 	if (error == 0 && uap->filename) {
5080 		error = nlookup_init(&nd, uap->filename, UIO_USERSPACE,
5081 				     NLC_FOLLOW);
5082 		if (error == 0)
5083 			error = nlookup(&nd);
5084 		if (error == 0)
5085 			error = cache_vref(&nd.nl_nch, nd.nl_cred, &vp);
5086 		nlookup_done(&nd);
5087 	}
5088 
5089 	if (error == 0 && uap->attrname) {
5090 		error = copyinstr(uap->attrname, attrname, EXTATTR_MAXNAMELEN,
5091 				  &size);
5092 	}
5093 
5094 	if (error == 0) {
5095 		error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
5096 		if (error == 0)
5097 			error = nlookup(&nd);
5098 		if (error == 0)
5099 			error = ncp_writechk(&nd.nl_nch);
5100 		if (error == 0) {
5101 			error = VFS_EXTATTRCTL(nd.nl_nch.mount, uap->cmd, vp,
5102 					       uap->attrnamespace,
5103 					       uap->attrname, nd.nl_cred);
5104 		}
5105 		nlookup_done(&nd);
5106 	}
5107 
5108 	return (error);
5109 }
5110 
5111 /*
5112  * Syscall to get a named extended attribute on a file or directory.
5113  */
5114 int
5115 sys_extattr_set_file(struct sysmsg *sysmsg,
5116 		     const struct extattr_set_file_args *uap)
5117 {
5118 	char attrname[EXTATTR_MAXNAMELEN];
5119 	struct nlookupdata nd;
5120 	struct vnode *vp;
5121 	struct uio auio;
5122 	struct iovec aiov;
5123 	int error;
5124 
5125 	error = copyin(uap->attrname, attrname, EXTATTR_MAXNAMELEN);
5126 	if (error)
5127 		return (error);
5128 
5129 	vp = NULL;
5130 
5131 	error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
5132 	if (error == 0)
5133 		error = nlookup(&nd);
5134 	if (error == 0)
5135 		error = ncp_writechk(&nd.nl_nch);
5136 	if (error == 0)
5137 		error = cache_vget(&nd.nl_nch, nd.nl_cred, LK_EXCLUSIVE, &vp);
5138 	if (error) {
5139 		nlookup_done(&nd);
5140 		return (error);
5141 	}
5142 
5143 	bzero(&auio, sizeof(auio));
5144 	aiov.iov_base = uap->data;
5145 	aiov.iov_len = uap->nbytes;
5146 	auio.uio_iov = &aiov;
5147 	auio.uio_iovcnt = 1;
5148 	auio.uio_offset = 0;
5149 	auio.uio_resid = uap->nbytes;
5150 	auio.uio_rw = UIO_WRITE;
5151 	auio.uio_td = curthread;
5152 
5153 	error = VOP_SETEXTATTR(vp, uap->attrnamespace, attrname,
5154 			       &auio, nd.nl_cred);
5155 
5156 	vput(vp);
5157 	nlookup_done(&nd);
5158 	return (error);
5159 }
5160 
5161 /*
5162  * Syscall to get a named extended attribute on a file or directory.
5163  */
5164 int
5165 sys_extattr_get_file(struct sysmsg *sysmsg,
5166 		     const struct extattr_get_file_args *uap)
5167 {
5168 	char attrname[EXTATTR_MAXNAMELEN];
5169 	struct nlookupdata nd;
5170 	struct uio auio;
5171 	struct iovec aiov;
5172 	struct vnode *vp;
5173 	int error;
5174 
5175 	error = copyin(uap->attrname, attrname, EXTATTR_MAXNAMELEN);
5176 	if (error)
5177 		return (error);
5178 
5179 	vp = NULL;
5180 
5181 	error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
5182 	if (error == 0)
5183 		error = nlookup(&nd);
5184 	if (error == 0)
5185 		error = cache_vget(&nd.nl_nch, nd.nl_cred, LK_SHARED, &vp);
5186 	if (error) {
5187 		nlookup_done(&nd);
5188 		return (error);
5189 	}
5190 
5191 	bzero(&auio, sizeof(auio));
5192 	aiov.iov_base = uap->data;
5193 	aiov.iov_len = uap->nbytes;
5194 	auio.uio_iov = &aiov;
5195 	auio.uio_iovcnt = 1;
5196 	auio.uio_offset = 0;
5197 	auio.uio_resid = uap->nbytes;
5198 	auio.uio_rw = UIO_READ;
5199 	auio.uio_td = curthread;
5200 
5201 	error = VOP_GETEXTATTR(vp, uap->attrnamespace, attrname,
5202 				&auio, nd.nl_cred);
5203 	sysmsg->sysmsg_result = uap->nbytes - auio.uio_resid;
5204 
5205 	vput(vp);
5206 	nlookup_done(&nd);
5207 	return(error);
5208 }
5209 
5210 /*
5211  * Syscall to delete a named extended attribute from a file or directory.
5212  * Accepts attribute name.  The real work happens in VOP_SETEXTATTR().
5213  */
5214 int
5215 sys_extattr_delete_file(struct sysmsg *sysmsg,
5216 			const struct extattr_delete_file_args *uap)
5217 {
5218 	char attrname[EXTATTR_MAXNAMELEN];
5219 	struct nlookupdata nd;
5220 	struct vnode *vp;
5221 	int error;
5222 
5223 	error = copyin(uap->attrname, attrname, EXTATTR_MAXNAMELEN);
5224 	if (error)
5225 		return(error);
5226 
5227 	error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
5228 	if (error == 0)
5229 		error = nlookup(&nd);
5230 	if (error == 0)
5231 		error = ncp_writechk(&nd.nl_nch);
5232 	if (error == 0) {
5233 		error = cache_vget(&nd.nl_nch, nd.nl_cred, LK_EXCLUSIVE, &vp);
5234 		if (error == 0) {
5235 			error = VOP_SETEXTATTR(vp, uap->attrnamespace,
5236 					       attrname, NULL, nd.nl_cred);
5237 			vput(vp);
5238 		}
5239 	}
5240 	nlookup_done(&nd);
5241 	return(error);
5242 }
5243 
5244 /*
5245  * Determine if the mount is visible to the process.
5246  */
5247 static int
5248 chroot_visible_mnt(struct mount *mp, struct proc *p)
5249 {
5250 	struct nchandle nch;
5251 
5252 	/*
5253 	 * Traverse from the mount point upwards.  If we hit the process
5254 	 * root then the mount point is visible to the process.
5255 	 */
5256 	nch = mp->mnt_ncmountpt;
5257 	while (nch.ncp) {
5258 		if (nch.mount == p->p_fd->fd_nrdir.mount &&
5259 		    nch.ncp == p->p_fd->fd_nrdir.ncp) {
5260 			return(1);
5261 		}
5262 		if (nch.ncp == nch.mount->mnt_ncmountpt.ncp) {
5263 			nch = nch.mount->mnt_ncmounton;
5264 		} else {
5265 			nch.ncp = nch.ncp->nc_parent;
5266 		}
5267 	}
5268 
5269 	/*
5270 	 * If the mount point is not visible to the process, but the
5271 	 * process root is in a subdirectory of the mount, return
5272 	 * TRUE anyway.
5273 	 */
5274 	if (p->p_fd->fd_nrdir.mount == mp)
5275 		return(1);
5276 
5277 	return(0);
5278 }
5279 
5280 /* Sets priv to PRIV_ROOT in case no matching fs */
5281 static int
5282 get_fspriv(const char *fsname)
5283 {
5284 
5285 	if (strncmp("null", fsname, 5) == 0) {
5286 		return PRIV_VFS_MOUNT_NULLFS;
5287 	} else if (strncmp(fsname, "tmpfs", 6) == 0) {
5288 		return PRIV_VFS_MOUNT_TMPFS;
5289 	}
5290 
5291 	return PRIV_ROOT;
5292 }
5293 
5294 int
5295 sys___realpath(struct sysmsg *sysmsg, const struct __realpath_args *uap)
5296 {
5297 	struct nlookupdata nd;
5298 	char *rbuf;
5299 	char *fbuf;
5300 	ssize_t rlen;
5301 	int error;
5302 
5303 	/*
5304 	 * Invalid length if less than 0.  0 is allowed
5305 	 */
5306 	if ((ssize_t)uap->len < 0)
5307 		return EINVAL;
5308 
5309 	rbuf = NULL;
5310 	fbuf = NULL;
5311 	error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
5312 	if (error)
5313 		goto done;
5314 
5315 	nd.nl_flags |= NLC_SHAREDLOCK;
5316 	error = nlookup(&nd);
5317 	if (error)
5318 		goto done;
5319 
5320 	if (nd.nl_nch.ncp->nc_vp == NULL) {
5321 		error = ENOENT;
5322 		goto done;
5323 	}
5324 
5325 	/*
5326 	 * Shortcut test for existence.
5327 	 */
5328 	if (uap->len == 0) {
5329 		error = ENAMETOOLONG;
5330 		goto done;
5331 	}
5332 
5333 	/*
5334 	 * Obtain the path relative to the process root.  The nch must not
5335 	 * be locked for the cache_fullpath() call.
5336 	 */
5337 	if (nd.nl_flags & NLC_NCPISLOCKED) {
5338 		nd.nl_flags &= ~NLC_NCPISLOCKED;
5339 		cache_unlock(&nd.nl_nch);
5340 	}
5341 	error = cache_fullpath(curproc, &nd.nl_nch, NULL, &rbuf, &fbuf, 0);
5342 	if (error)
5343 		goto done;
5344 
5345 	rlen = (ssize_t)strlen(rbuf);
5346 	if (rlen >= uap->len) {
5347 		error = ENAMETOOLONG;
5348 		goto done;
5349 	}
5350 	error = copyout(rbuf, uap->buf, rlen + 1);
5351 	if (error == 0)
5352 		sysmsg->sysmsg_szresult = rlen;
5353 done:
5354 	nlookup_done(&nd);
5355 	if (fbuf)
5356 		kfree(fbuf, M_TEMP);
5357 
5358 	return error;
5359 }
5360 
5361 int
5362 sys_posix_fallocate(struct sysmsg *sysmsg, const struct posix_fallocate_args *uap)
5363 {
5364 	return (kern_posix_fallocate(uap->fd, uap->offset, uap->len));
5365 }
5366 
5367 int
5368 kern_posix_fallocate(int fd, off_t offset, off_t len)
5369 {
5370 	struct thread *td = curthread;
5371 	struct vnode *vp;
5372 	struct file *fp;
5373 	int error;
5374 
5375 	if (offset < 0 || len <= 0)
5376 		return (EINVAL);
5377 	/* Check for wrap. */
5378 	if (offset > OFF_MAX - len)
5379 		return (EFBIG);
5380 
5381 	fp = holdfp(td, fd, -1);
5382 	if (fp == NULL)
5383 		return (EBADF);
5384 
5385 	switch (fp->f_type) {
5386 	case DTYPE_VNODE:
5387 		break;
5388 	case DTYPE_PIPE:
5389 	case DTYPE_FIFO:
5390 		error = ESPIPE;
5391 		goto out;
5392 	default:
5393 		error = ENODEV;
5394 		goto out;
5395 	}
5396 
5397 	if ((fp->f_flag & FWRITE) == 0) {
5398 		error = EBADF;
5399 		goto out;
5400 	}
5401 
5402 	vp = fp->f_data;
5403 	if (vp->v_type != VREG) {
5404 		error = ENODEV;
5405 		goto out;
5406 	}
5407 
5408 	vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
5409 	error = VOP_ALLOCATE(vp, offset, len);
5410 	vn_unlock(vp);
5411 out:
5412 	dropfp(td, fd, fp);
5413 	return (error);
5414 }
5415